---
title: "float equal"
author: "xserver"
date: "2013-05-23T18:21:39Z"
tags: []
categories: ["编程语言"]
slug: "float-equal"
draft: false
lastmod: "2013-05-23T18:21:39Z"
id: 1874270
url: "https://xserver.iteye.com/blog/1874270"
---

- (BOOL)floatA:(float)f1 equalB:(float)f2
{
    const float EPSINON = 0.00001;
    if ((f1 > 0.0f && f2 > 0.0f) || (f1 < 0.0f && f2 < 0.0f))
    {
        float x = fabsf(f1) - fabsf(f2);
        if (x >= -EPSINON && x <= EPSINON) {
            return YES;
        }
    }

    return NO;
}


别用乘法放大为整形比较...本质是不能比较，计算机浮点数精度问题。