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

[fuzzing] std.math.sin vs glibc sin/sinf disagreements #9902

Closed
squeek502 opened this issue Oct 6, 2021 · 4 comments · Fixed by #10276
Closed

[fuzzing] std.math.sin vs glibc sin/sinf disagreements #9902

squeek502 opened this issue Oct 6, 2021 · 4 comments · Fixed by #10276
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@squeek502
Copy link
Collaborator

squeek502 commented Oct 6, 2021

These are cases found via https://github.com/squeek502/zig-std-lib-fuzzing where the Zig implementation and the glibc implementation of sin disagree on the results. Note that these could either be a Zig bug or a glibc bug. Also note that this list should not be considered exhaustive.

Format for each case is (can be either f32 or f64, will be differentiated by the width of the binary notation):

in : <float input to sin in binary notation>
<zig testing.expectEqual error>
zig: <zig's result in binary notation>
c  : <glibc's result in binary notation>
(only when the musl and glibc results are different):
m  : <musl's result in binary notation>
mf : <musl's result in default zig fmt notation>

Cases:

in : 0100000000000000000000000000000000000000000000000001000000000000
expected 9.092974268249248e-01, found 9.092974268249246e-01
zig: 0011111111101101000110001111011011101010110100011001100110100011
c  : 0011111111101101000110001111011011101010110100011001100110100100

in : 01000000000000001010011010100110
expected 9.05017673e-01, found 9.05017614e-01
zig: 00111111011001111010111100111100
c  : 00111111011001111010111100111101

in : 01001000011101101110100000000000
expected 2.33006611e-01, found 2.28855669e-01
zig: 00111110011010100101100100100100
c  : 00111110011011101001100101001001

in : 01001110011100101010000000000000
expected 1.63289263e-01, found 1.85135078e+05
zig: 01001000001101001100101111000101
c  : 00111110001001110011010101001101

in : 01001110011100101001111111111111
expected -8.43691587e-01, found 7.34320759e+14
zig: 01011000001001101111011100011000
c  : 10111111010101111111110000101100

in : 0100001100000000000000001110000000000000000000000000000000000000
expected 9.08591047718329e-01, found 9.120429664891301e-01
zig: 0011111111101101001011110111010010111011001100111100001110110000
c  : 0011111111101101000100110010110110001000011011000110111101100110

in : 0100000000001100111111110111100000000000000000000000000000000100
expected -4.645690959179299e-01, found -4.6456909591792994e-01
zig: 1011111111011101101110111000000000000100011011001100100101110110
c  : 1011111111011101101110111000000000000100011011001100100101110101

in : 01001110010100111010000000000000
expected 6.90152049e-01, found 2.1922186e+07
zig: 01001011101001110100000011000101
c  : 00111111001100001010110111001110

in : 01001110000000001101001011111111
expected -9.81242120e-01, found 4.97343365e+10
zig: 01010001001110010100011001011100
c  : 10111111011110110011001010101111

in : 1100001101111111011111110111111101111111100000001100001100000000
expected 8.004099288476395e-01, found -2.023617463869512e+06
zig: 1100000100111110111000001100000101110110110000000010011100000000
c  : 0011111111101001100111001111010101001000011110010110101011100101

in : 01001110010011100100111000000000
expected -3.11624944e-01, found 2.33093351e+15
zig: 01011001000001000111111110001100
c  : 10111110100111111000110101001110

in : 11001010000000000001011011111010
expected -7.88453161e-01, found -8.24845671e-01
zig: 10111111010100110010100100010110
c  : 10111111010010011101100000010001

in : 11001101000000011111111100000000
expected -4.27663803e-01, found -2.34546905e+02
zig: 11000011011010101000110000000010
c  : 10111110110110101111011011000000

in : 11001101110011011100110111001101
expected -1.20696187e-01, found -2.71583002e+11
zig: 11010010011111001110111001101111
c  : 10111101111101110010111110010000

in : 01001110000010000001100111111111
expected 3.92203092e-01, found -6.49677952e+08
zig: 11001110000110101110010100110010
c  : 00111110110010001100111011011000

in : 1100001111000011110000110000000000000000000000000000000000000000
expected -6.778564488857252e-01, found -1.8795122100475533e+25
zig: 1100010100101111000110000000101100111000101010100010101110001001
c  : 1011111111100101101100010000000000000001111010110001100110101111

in : 01001110010100111001111111111111
expected 9.36231136e-01, found 1.98054632e+15
zig: 01011000111000010010100101111100
c  : 00111111011011111010110011011000

in : 1100001111000011110000111100001100000000000000011100001111000011
expected -8.08603289849552e-01, found 2.4952947336399975e+10
zig: 0100001000010111001111010011111100001010001000011001100110010011
c  : 1011111111101001111000000001010000000001101010101111000001001111

in : 01001101000110100101111111111111
expected -1.30424886e-01, found 3.17445526e+01
zig: 01000001111111011111010011011000
c  : 10111110000001011000111000011010

in : 0100001101111011010000110000000000000100110100110111101101000011
expected -3.4213862981519044e-01, found 2.096661263497096e+03
zig: 0100000010100000011000010101001010010001001000010000110000100010
c  : 1011111111010101111001011001100101101100011100000100100110011111

in : 11001101110000011100110111111111
expected 8.37280929e-01, found -1.25460615e+04
zig: 11000110010001000000100000111111
c  : 00111111010101100101100000001011

in : 01001110100101011110001100000000
expected -8.34248661e-01, found 9.39783136e+13
zig: 01010110101010101111001000010000
c  : 10111111010101011001000101010010

in : 11001101010110000110010001100100
expected -5.45474529e-01, found 9.02290344e-01
zig: 00111111011001101111110010000000
c  : 10111111000010111010010000111000

in : 11001110110000000000000110100101
expected 6.71918809e-01, found -2.12447159e+12
zig: 11010011111101110101001000110000
c  : 00111111001011000000001011011111

in : 0100001111001110110111000010000000000000000000111101110000111110
expected -7.869679179222414e-01, found 1.0058259276925056e+20
zig: 0100010000010101110011110111010010011100110110110101101111010000
c  : 1011111111101001001011101101011101010111110011110100010110001011

in : 11001110000101111100111010000000
expected 8.48641514e-01, found -8.90999705e+09
zig: 11010000000001001100010011110001
c  : 00111111010110010100000010010010

in : 0100001110111111100000000000000000000100101111110101000001111010
expected -9.652505668435394e-01, found 6.946386659742551e+23
zig: 0100010011100010011000110000111000000000010011011100011001100101
c  : 1011111111101110111000110101010100101000001000010011101100011000
m  : 1011111111101110111000110101010100101000001000010011101100011001
mf : -9.652505668435395e-01

in : 01001010010010100001000011110011
expected -1.0e+00, found -9.95873391e-01
zig: 10111111011111101111000110001111
c  : 10111111100000000000000000000000

in : 1100001110100111101001111010011110100111101001111010011110100111
expected 9.203011777144383e-01, found -1.8897266312835092e+16
zig: 1100001101010000110010001011110110101011011100110101110000000101
c  : 0011111111101101011100110001101101110100100110000001111100011010
@squeek502
Copy link
Collaborator Author

Just checked each input against musl's implementation and it agrees with glibc in every case except 1, where it differs only by 1 bit (the 0100001110111111100000000000000000000100101111110101000001111010 input case). Added that info the OP.

@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. standard library This issue involves writing Zig code for the standard library. labels Oct 6, 2021
@andrewrk andrewrk added this to the 0.10.0 milestone Oct 6, 2021
@tiehuis
Copy link
Member

tiehuis commented Oct 6, 2021

As a note, while most of our math library is ported from musl there a few functions which were ported from go. Sin, Cos and Tan in particular. The results shouldn't be so far off as they are, but should keep in mind for the very close cases as the method/implementation may differ a bit.

@marler8997
Copy link
Contributor

Watch out Zig, "the fuzz" is keeping an eye on you

@squeek502
Copy link
Collaborator Author

Turns out this is essentially a duplicate of #9901, as each of the cases here trigger the reduceThreshold path in the Go code and the Go implementation gives results that agree with the libc implementation (give or take a few bits).

Going to leave it open for now, but fixing #9901 will almost certainly fix this issue as well.

tiehuis added a commit to tiehuis/zig that referenced this issue Dec 5, 2021
@tiehuis tiehuis linked a pull request Dec 5, 2021 that will close this issue
@andrewrk andrewrk modified the milestones: 0.11.0, 0.9.0 Dec 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants