0

I am trying to write some code that computes whether a is less than b with n-bit tolerance, where a and b are double precision variables.

For example, 4.000000001 < 4.00000001 would be TRUE, but 4.0000000001 < 4.000000001 would be FALSE. 3.99999999 < 4.00000000 would also be FALSE. Also note that a and b could be negative.

Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70

2 Answers2

3

This should work:

ndigits = 7;
round(a*10^ndigits) < round(b*10^ndigits) 
Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
2

If you really look for a < relationship, I would do

x < y + tolerance

where tolerance is a value which indicates which values above y shall count as below it.

glglgl
  • 89,107
  • 13
  • 149
  • 217