planttheidea / fast-equals

A blazing fast equality comparison, either shallow or deep
MIT License
474 stars 20 forks source link

How to Custom Float Comparison? #112

Open ozum opened 11 months ago

ozum commented 11 months ago

Hi,

I need to use a custom comparison function for numbers for my use case. This function returns true for equality check when two floating point numbers are very close, even if they are not equal. Below is a simple implementation:

function isNumberEqual(a: number, b: number): boolean {
  if (a === b) return true;
  const diff = Math.abs(a - b);
  if (diff < Number.EPSILON) return true;
  return diff <= Number.EPSILON * Math.min(Math.abs(a), Math.abs(b));
}

How can I use this function in numeric comparisons in fast-equals?

Many thanks,

planttheidea commented 1 month ago

To-date, numbers have been treated as true primitives, and explicitly used SameValueZero comparison for equality checks without an override. However, I can see the use-case for having custom comparators for primitives, such as the numeric float example you provide. I can add arePrimitivesEqual support.