Machine Epsilon

I’m porting a Java physics engine to Xojo.

In the engine, epsilon is used in a lot of calculations. It’s determined within a computed property:

Public Shared Property E as Double
Get
  Static e_ As Double = Compute
  
  Return e_
End Get
End Property
Public Shared Function Compute() as Double
  Var e As Double = 0.5
  
  While (1.0 + e > 1.0)
    e = e * 0.5
  Wend
  
  Return e
End Function

This works fine. On my iMac Pro it consistently returns the value 0.0000000000000001 which I believe is 2-53. Am I right in thinking that this will always be the case on every platform that Xojo supports since (according to this article) that is the machine epsilon for double precision floats? If so, it means I can put that value in a constant and save a computed property access. This adds up as the computed property is accessed millions of times.

In C float.h I found this:

#define DBL_EPSILON 2.2204460492503131e-16

This may be the same number as you got there.

1 Like

AFAICT yes it should be absolutely fixed since Xojo uses IEEE 754 double precision floats

1 Like

If you want to ensure your epsilon is valid for the current hardware/platform (now and in the future), why not compute it once at app startup and store it in a global variable?

The static in the computed property should behave that way already
Compute it once then just return it over & over
However, billions of accesses to the Get method may be slower than billions of access to a property on the app class or in a modules globals

I think Christian tested that once way back when

Yep - it’s definitely slower. I’ve tested it.

Jays suggestion of a property on app or a global variable makes a ton of sense JUST to optimize the heck out of this

Tip of day: Make variables in app shared

1 Like