-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide the full set of IEEE Operations/Behaviors required for compliance #27204
Comments
CC. @danmosemsft, @eerhardt I went through the IEEE 754:2008 manual and attempted to track all required/recommended operations above. We should probably open individual issues (where one does not already exist) to track any sub behaviors/features/operations that are not currently exposed. Also CC. @CarolEidt, who may be interested in some of this. |
It should be noted that, while I made a good effort to ensure the table is accurate, I may have missed bits (either operations from the spec or missed something already supported by .NET). |
Logged https://github.com/dotnet/corefx/issues/31902 to track the missing rounding directions. |
Logged https://github.com/dotnet/corefx/issues/31903 to track several of the missing "required" operations. |
|
It is also worth noting that for many of these operations, doing a two-step "equivalent" operation is not always equivalent. Since individual operations are documented to compute to the "correct infinitely precise" result, and then round using the current (or explicitly specified) rounding direction. As an example, |
@tannergooding - thanks for the great detail on this! |
@tannergooding if we are making a list of discrepancies with IEEE754, we should note that modulo of floating point operands is not compliant hence the existence of alternative |
Updated above with some of the things that have been done since the issue was created. |
I've updated this to include the changes for IEEE 754:2019 from #1387 |
Worth adding links where issues exist? eg to #936 |
Yes, definitely. I will give this another pass tomorrow. |
About that, I think not many people are very aware of what IEEE 754 (and versions) is, let alone the implication, or what they are missing out on ;). People also have learned that working with floats is kinda dangerous, esp. w.r.t. comparisons and equality and/or in relation to (non-signaling) For instance, being able to trap signaling Another source for concern in the current implementation of min/max and sorting is that different parts of .NET use different approaches. I.e., LINQ returns different results depending on whether Fixing nan.CompareTo(Math.CopySign(nan, 1.0)) // yields 0
nan.CompareTo(Math.CopySign(nan, -1.0)) // yields 0
(2.0).CompareTo(Math.CopySign(nan, 1.0)) // yields 1
(2.0).CompareTo(Math.CopySign(nan, -1.0)) // yields 1 I'd assume it behaves the same if I set the signaling bit or anything else of the free bits in
Being able to set the default binding direction for a thread/process or assembly would be of great use for a larger group of users, as often I notice that in real-world code, not all people on the team know of the preferred rounding in a code-base and deviate from the course, leading to issues downstream.
For a long time I've personally considered these big misses, as it is often required to round towards zero, or round towards an infinity. Java has had these for a very long time and people often ask about this. EDIT: I think this issue is a little behind on updates (@tannergooding, what did we miss? ;) ), apparently, quite recently, Also many thanks for adding things like I hope that adding the other mathematical functions in the list are going to make it in .NET 7.0 or soon after. Here's hoping! |
Just noticed this excellent historical summary by @tannergooding , showing the evolution of the IEEE functions (and quite a bit more). Pity it’s so buried, it’s an excellent post, worthy of a blog! dotnet/csharplang#2585 (comment) |
Updated this tracking issue with some features we added via Generic Math, should be more or less up to date now. |
Should not implement it by now. float128 is not natively hardware-supported, c or c++ in GCC uses software calculatte instead. Considering of the instruction cycle, There is a lot of performance issues. This asm code is c++ generated with -O3 optmization .
When this is how GCC add up two double value.
|
Not all types are designed for performance, many are designed for handling specific edge cases or other needs and are balanced for usability instead. |
Closing this. We've exposed the full set of required operations now and many optional ones on top. Additional things can be done via opening individual proposals where appropriate. |
Rationale
The IEEE 754:2008 and IEEE 754:2019 spec define a number of operations (some required, some recommended, and some optional) and the .NET Framework exposes a good number, but not all of them.
It would be ideal to ensure that all required operations are properly exposed and that they are compliant with the IEEE spec. Where a recommended or optional operation is also exposed, we should ensure that it is also compliant. Where a behavior/operation/feature is explicitly not implemented, we should ensure that it is properly documented with any reasoning as to why it is not supported.
For the purposes of this proposal, this document only covers the
binary
operations. The IEEE spec also defines a format and set of operations fordecimal
values, but those are not supported by .NET in any form today and should be covered separately.Additionally, this general proposal is meant to track the overall status, and sub-proposals, where applicable should be opened to cover specific behaviors/operations. This proposal does not go in detail as to any special behaviors or handling of individual operations, which should be checked and validated separately.
Encodings
Support for all formats and representations is language/implementation-defined. .NET currently supports
binary32
andbinary64
. There have also been requests forbinary16
,binary128
, and arbitrarily sizedbinary{k}
.The basic encodings defined are:
System.Single
System.Double
The extended encodings defined are:
System.Half
k >= 128
and is a multiple of 32Attributes
Several attributes that control the semantics of a "block" are given. For any exposed attribute, you must provide a means of statically setting the default and each support value. Providing a means to dynamically set the attributes is recommended.
Required
System.MidpointRounding.ToEven
System.MidpointRounding.AwayFromZero
Recommended
Optional
If setting the attributes dynamically is supported, the following are required
Required Operations
All operations listed below are considered required
General (T is a floating-point format)
Math.Round
,Math.Round w/ MidpointRounding.ToEven
Math.Round w/ MidpointRounding.AwayFromZero
Math.Round w/ MidpointRounding.ToZero
Math.Round w/ MidpointRounding.ToPositiveInfinity
Math.Round w/ MidpointRounding.ToNegativeInfinity
Rounding Direction
attributeMath.BitIncrement
Math.BitDecrement
Math.IEEERemainder
logBFormat operations (T is a floating-point format, U can be an integral format or the same type as T)
Math.ScaleB
Math.LogB
Arithmetic
x + y
x - y
x * y
x / y
Math.Sqrt
Math.FusedMultiplyAdd
(T)x
(integerFormat)x
, Not supported as an explicit operationConversion
(U)x
where U is another floating-point formatDouble.Parse
Double.ToString
,Double.ToString w/ format
Sign Bit
var y = x
-x
Math.Abs
Math.CopySign
Comparison
x == y
x != y
x > y
x >= y
x < y
x <= y
Conformance
General
Double.IsNegative
Double.IsNormal
Double.IsFinite
INumberBase.IsZero, but not directly exposed
Double.IsSubnormal
Double.IsInfinity
Double.IsNaN
INumberBase.IsCanonical, but not directly exposed
INumberBase.Radix, but not directly exposed
Flags
Exceptions
Exceptions are not currently supported by the CoreCLR and no mechanism to control them is currently made available. I have considerd them "out of scope" for the purposes of this document.
Recommended Operations
Additional Mathematical Operations
Double.Exp
Double.ExpM1
Double.Exp2
Double.Exp2M1
Double.Exp10
Double.Exp10M1
Math.Log
Math.Log2
Math.Log10
Double.LogP1
Double.Log2P1
Double.Log10P1
Double.Hypot
Double.RootN
Math.Pow
Math.Sin
Math.Cos
Math.Tan
Double.SinPi
Double.CosPi
Double.TanPi
Math.Asin
Math.Acos
Math.Atan
Math.Atan2
Double.ASinPi
Double.ACosPi
Double.ATanPi
Double,ATan2Pi
Math.Sinh
Math.Cosh
Math.Tanh
Math.Asinh
Math.Acosh
Math.Atanh
Reduction
Augmented Arithmetic
Minimum and Maximum
Math.Min
Double.MinNumber
Math.Max
Double.MaxNumber
Math.MinMagnitude
Double.MinMagnitudeNumber
Math.MaxMagnitude
Double.MaxMagnitudeNumber
NaN
The text was updated successfully, but these errors were encountered: