Skip to content
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

Support UntypedBigdecType, BigdecType, Float32Type and Float64Type. #306

Merged
merged 22 commits into from
Aug 5, 2022

Conversation

jaekwon
Copy link
Contributor

@jaekwon jaekwon commented Aug 2, 2022

UPDATE2:

Currently, BigdecType is almost arbitrary precision, except for hex_p notation (whatever it's called).
I don't understand how you can support numbers like 0x1p-53 without loss of precision, without some tricky system to manage precision across operations. Instead, it's supported to a precision of around 1024bits. Multiplication and division also have a precision of about 1024 digits. To be completely spec compatible, we might need to use big.Float or some other arbitrary precision library just to hold hex_p numbers, but I think it's better to keep it simple as is and just document the difference.

After this PR, it will become possible to upload the flowrate library with some simple modifications: (a) remove sync mutexes (for now, sync is not supported), and (b) remove time.Sleep() synchronous API.


UPDATE: if we don't need to worry about fusing *+, and if we panic upon NaN, and we don't use sin/cos etc, is there any nondeterminism at all? For now this PR just implements native operations. Meanwhile this article has a lot of references to cross check: https://gafferongames.com/post/floating_point_determinism/

Some insights:


For reference: https://stackoverflow.com/questions/2186788/is-there-an-open-source-c-c-implementation-of-ieee-754-operations, softfloat looks pretty good.

For now, since there are no other clear options for Go, we will use math/big.Float:

By setting the desired precision to 24 or 53 and using matching rounding mode (typically ToNearestEven), Float operations produce the same results as the corresponding float32 or float64 IEEE-754 arithmetic for operands that correspond to normal (i.e., not denormal) float32 or float64 numbers. Exponent underflow and overflow lead to a 0 or an Infinity for different values than IEEE-754 because Float exponents have a much larger range. (https://pkg.go.dev/math/big#Float.Float64)

@moul moul marked this pull request as draft August 2, 2022 06:51
@moul
Copy link
Member

moul commented Aug 4, 2022

So happy about native time, and Bigdec!

@jaekwon
Copy link
Contributor Author

jaekwon commented Aug 4, 2022

Only UntypedBigDec is used right now (which are only made by constant expressions or constant literals) which then gets converted to float32 or float64 upon usage, but it should be easy to also support bigdec in the future if we want.
There are costs associated with bigint, bigdec operations btw, so it might make more sense to support decimal64 etc first.

@jaekwon jaekwon changed the title WIP: initial commit for float32 and float64 support Support UntypedBigdecType, BigdecType, Float32Type and Float64Type. Aug 4, 2022
@jaekwon jaekwon requested a review from moul August 4, 2022 17:10
@jaekwon jaekwon marked this pull request as ready for review August 4, 2022 19:19
@@ -67,11 +68,13 @@ func RunFileTest(rootDir string, path string, nativeLibs bool, logger loggerFunc
stdin := new(bytes.Buffer)
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
filesPath := "./files"
filesPath := "./files2"
mode := ImportModeStdlibsPreferred
if nativeLibs {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was weird. looks like before there was a bug where the native functions were only being run in files2 (when it should be so for files1 instead), and somehow the tests were still passing anyways? well, either way, the intent was this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants