Skip to main content
Stack Overflow for Teams is now Stack Internal: See how we’re powering the human intelligence layer of enterprise AI. Read more >
added 111 characters in body
Source Link
fulmicoton
  • 16.1k
  • 10
  • 56
  • 74

The code you wrote is bugged :

return (diff < EPSILON) && (-diff > EPSILON);

The correct code would be :

return (diff < EPSILON) && (diff > -EPSILON);

(...and yes this is different)

I wonder if fabs wouldn't make you lose lazy evaluation in some case. I would say it depends on the compiler. You might want to try both. If they are equivalent in average, take the implementation with fabs.

If you have some info on which of the two float is more likely to be bigger than then other, you can play on the order of the comparison to take better advantage of the lazy evaluation.

Finally you might get better result by inlining this function. Not likely to improve much though...

Edit: OJ, thanks for correcting your code. I erased my comment accordingly

The code you wrote is bugged :

return (diff < EPSILON) && (-diff > EPSILON);

The correct code would be :

return (diff < EPSILON) && (diff > -EPSILON);

(...and yes this is different)

I wonder if fabs wouldn't make you lose lazy evaluation. I would say it depends on the compiler.

If you have some info on which of the two float is more likely to be bigger than then other, you can play on the order of the comparison to take better advantage of the lazy evaluation.

Finally you might get better result by inlining this function. Not likely to improve much though...

Edit: OJ, thanks for correcting your code. I erased my comment accordingly

The code you wrote is bugged :

return (diff < EPSILON) && (-diff > EPSILON);

The correct code would be :

return (diff < EPSILON) && (diff > -EPSILON);

(...and yes this is different)

I wonder if fabs wouldn't make you lose lazy evaluation in some case. I would say it depends on the compiler. You might want to try both. If they are equivalent in average, take the implementation with fabs.

If you have some info on which of the two float is more likely to be bigger than then other, you can play on the order of the comparison to take better advantage of the lazy evaluation.

Finally you might get better result by inlining this function. Not likely to improve much though...

Edit: OJ, thanks for correcting your code. I erased my comment accordingly

I confirm that my comment was only valid for C, sorry for that.
Source Link
fulmicoton
  • 16.1k
  • 10
  • 56
  • 74

I don't know any, but about OJ's comment, abs is for int, the float version is called "fabs". If OJ's compile, you may get really scary result.

Beware!

Plus, theThe code you wrote is bugged :

return (diff < EPSILON) && (-diff > EPSILON);

The correct code would be :

return (diff < EPSILON) && (diff > -EPSILON);

(...and yes this is different)

I wonder if fabs wouldn't make you lose lazy evaluation. I would say it depends on the compiler.

If you have some info on which of the two float is more likely to be bigger than then other, you can play on the order of the comparison to take better advantage of the lazy evaluation.

Finally you might get better result by inlining this function. Not likely to improve much though...

Edit: OJ, thanks for correcting your code. I erased my comment accordingly

I don't know any, but about OJ's comment, abs is for int, the float version is called "fabs". If OJ's compile, you may get really scary result.

Beware!

Plus, the code you wrote is bugged :

return (diff < EPSILON) && (-diff > EPSILON);

The correct code would be :

return (diff < EPSILON) && (diff > -EPSILON);

(...and yes this is different)

If you have some info on which of the two float is more likely to be bigger than then other, you can play on the order of the comparison to take better advantage of the lazy evaluation.

Finally you might get better result by inlining this function.

The code you wrote is bugged :

return (diff < EPSILON) && (-diff > EPSILON);

The correct code would be :

return (diff < EPSILON) && (diff > -EPSILON);

(...and yes this is different)

I wonder if fabs wouldn't make you lose lazy evaluation. I would say it depends on the compiler.

If you have some info on which of the two float is more likely to be bigger than then other, you can play on the order of the comparison to take better advantage of the lazy evaluation.

Finally you might get better result by inlining this function. Not likely to improve much though...

Edit: OJ, thanks for correcting your code. I erased my comment accordingly

added 62 characters in body
Source Link
fulmicoton
  • 16.1k
  • 10
  • 56
  • 74

I don't know any, but about OJ's comment, abs is for int, the float version is called "fabs". If OJ's compile, you may get really scary result.

Beware!

Plus, the code you wrote is bugged :

return (diff < EPSILON) && (-diff > EPSILON);

The correct code would be :

return (diff < EPSILON) && (diff > -EPSILON);

(...and yes this is different)

Finally, ifIf you have some info on which of the two float is more likely to be bigger than then other, you can play on the order of the comparison to take better advantage of the lazy evaluation.

Finally you might get better result by inlining this function.

I don't know any, but about OJ's comment, abs is for int, the float version is called "fabs". If OJ's compile, you may get really scary result.

Beware!

Plus, the code you wrote is bugged :

return (diff < EPSILON) && (-diff > EPSILON);

The correct code would be :

return (diff < EPSILON) && (diff > -EPSILON);

(...and yes this is different)

Finally, if you have some info on which of the two float is more likely to be bigger than then other, you can play on the order of the comparison to take better advantage of the lazy evaluation.

I don't know any, but about OJ's comment, abs is for int, the float version is called "fabs". If OJ's compile, you may get really scary result.

Beware!

Plus, the code you wrote is bugged :

return (diff < EPSILON) && (-diff > EPSILON);

The correct code would be :

return (diff < EPSILON) && (diff > -EPSILON);

(...and yes this is different)

If you have some info on which of the two float is more likely to be bigger than then other, you can play on the order of the comparison to take better advantage of the lazy evaluation.

Finally you might get better result by inlining this function.

Source Link
fulmicoton
  • 16.1k
  • 10
  • 56
  • 74
Loading