QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: EricE on March 16, 2020, 01:22:04 pm

Title: Machine Epsilon for QB64
Post by: EricE on March 16, 2020, 01:22:04 pm
What value can be used for the QB64's Machine Epsilon for DOUBLE variables?

This would be a value such that the DOUBLE variable x value would be considered zero if the following criteria is met.

ABS(x) < EPSILON

From the Wiki article on Machine Epsilon,
https://en.wikipedia.org/wiki/Machine_epsilon (https://en.wikipedia.org/wiki/Machine_epsilon)

we get code that approximates this value to within a factor of two.
Translated into QB64 the code is:

Code: QB64: [Select]
  1. DIM epsilon AS DOUBLE
  2.  
  3. epsilon = 1.0
  4.  
  5. WHILE (1.0 + 0.5 * epsilon) <> 1.0
  6.     epsilon = 0.5 * epsilon
  7. PRINT epsilon
  8.  

When this code is run on a Windows 10 64-bit computer using the 32-bit version of QB64, this value is computed.

epsilon = 1.192092895507812D-7
Title: Re: Machine Epsilon for QB64
Post by: EricE on March 16, 2020, 01:44:32 pm
If we change the code to converge towards zero instead of one, we get a smaller value.
epsilon = 1.401298464324817D-45
This might be the better value to use when testing if a determinant value is zero.

Code: QB64: [Select]
  1. DIM epsilon AS DOUBLE
  2.  
  3. epsilon = 1.0
  4.  
  5. WHILE (0.5 * epsilon) <> 0.0
  6.     epsilon = 0.5 * epsilon
  7. PRINT epsilon
  8.  
  9.