Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

double.Epsilon vs. std::numeric_limits<double>::min()

Why double.Epsilon != std::numeric_limits<double>::min()?

On my PC: double.Epsilon == 4.9406564584124654E-324 and is defined in .NET std::numeric_limits<double>::min() == 2.2250738585072014e-308

Is there a way to get 2.2250738585072014e-308 from .NET?

like image 430
sthiers Avatar asked Mar 07 '26 09:03

sthiers


2 Answers

They're different because double.Epsilon returns the smallest representable value. numeric_limits<double>::min() returns the smallest normalized value.

Basically double.Epsilon is the equivalent to numeric_limits<double>::denorm_min().

The easiest way of getting the equivalent in .NET is probably to work out the bit pattern for the minimal normalized number and use BitConverter.Int64BitsToDouble.

like image 62
Jon Skeet Avatar answered Mar 08 '26 21:03

Jon Skeet


Well you could use C++/CLI to return the value:

double epsilon() { return std::numeric_limits<double>::min(); }

Why would you want to though? Why do they have to be the same? You should try to avoid skating on the edges of your floating point numbers.

like image 37
1800 INFORMATION Avatar answered Mar 08 '26 22:03

1800 INFORMATION