Open scharnyw opened 1 month ago
Tagging subscribers to this area: @dotnet/area-system-numerics See info in area-owners.md if you want to be subscribed.
The values can be exposed by IFloatingPoint
interface.
Note that the usage contains a pitfall. If you convert the value from the integer than testing with Max/MinSafeInteger, you may get MaxSafeInteger+1 rounded to MaxSafeInteger.
Note that the usage contains a pitfall. If you convert the value from the integer than testing with Max/MinSafeInteger, you may get MaxSafeInteger+1 rounded to MaxSafeInteger.
I believe you won't, because MaxSafeInteger
is not the maximum integer such that it and all smaller integers can be represented exactly in the floating point format. It is the maximum integer such that it can be represented exactly and can be compared correctly against all other integers. The latter is smaller than the former by one, i.e. for doubles, the former is 2^53 while the latter is 2^53-1.
Therefore both MaxSafeInteger
and MaxSafeInteger + 1
can be represented exactly in doubles, so pitfalls like this don't happen.
Background and motivation
I find it occasionally necessary to represent integral values with floating-point numeric types. Some reasons why this may be necessary include, but are not limited to:
In such cases it would be helpful to have the constants
MinSafeInteger
andMaxSafeInteger
defined in the floating-point numeric types to know if an integral value could be represented safely in the given type. "Safely" in this context refers to the ability to represent integers exactly and to compare them correctly.This proposal is identical in concept to the corresponding APIs in JavaScript.
API Proposal
API Usage
Alternative Designs
No response
Risks
No response