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

Floating point values. #1

Closed
BurntSushi opened this issue Feb 28, 2013 · 8 comments
Closed

Floating point values. #1

BurntSushi opened this issue Feb 28, 2013 · 8 comments

Comments

@BurntSushi
Copy link
Member

I believe the treatment of 64 bit integers in the test suite is solid, but I think testing floating point values has holes.

It is partially mitigated by the fact that all values are encoded as JSON strings. This means that we can interpret all floating point values as Go does and perform a proper equality test.

However, different languages may have different floating point representations. Which means that some languages might produce different values---which poses problems for an equality test.

In practice, this may not be so much of an issue since floating point conversion is typically well-handled by the language being used. However, it may be worth it to come up with tests for sufficiently small and large numbers that are not reliably represented in practice.

Overall, I'm not sure. And I have only a passing familiarity with the inner workings of floating point representation to know if this is a real issue or not.

@BurntSushi
Copy link
Member Author

Tip: For Python parsers, I used repr instead of str to convert a float value to a string.

@Julian
Copy link

Julian commented Apr 7, 2013

(Not sure what you mean about Python, if this is to interact with this suite, the json module will perfectly well serialize Python floats into JSON numbers).

@BurntSushi
Copy link
Member Author

@Julian It's much more subtle than that. This transcript might make things clearer:

>>> f = 3.141592653589793
>>> print str(f)
3.14159265359
>>> print repr(f)
3.141592653589793

@BurntSushi
Copy link
Member Author

Ah, I might see the source of your confusion now. JSON serialization doesn't really apply here, since the interface of the tester reads all primitive values as strings. So you can't just take a dictionary representation of the interface and dump it as JSON. You've got to make sure all your values are strings.

@Julian
Copy link

Julian commented Apr 7, 2013

My comment was before you pointed out that your interface is expecting strings. If that's true, then the json module will encode them as numbers, not strings, so you're up to converting them yourself. Which of str or repr is appropriate depends, but what should be going on is an approximate equality check, which should allow either.

@BurntSushi
Copy link
Member Author

but what should be going on is an approximate equality check, which should allow either.

What value of epsilon do you suggest I use?

@BurntSushi
Copy link
Member Author

It's not clear to me that this is a problem in practice. Moreover, an approximate equality check typically requires some notion of what one believes is accurate. I don't know what that means in this context.

If someone comes across this problem in the real world, please leave a comment and I'll re-open the issue.

uiri added a commit that referenced this issue Jul 3, 2018
@mmakaay
Copy link

mmakaay commented Jun 29, 2019

Not an epsilon-related issue, but an issue related to comparing float values.

When comparing "nan" floats, parsing to a float64 and comparing the expected and actual version using == will not work (it will always return false). You'll have to check if both values are NaN using math.IsNaN(floatValue). In my codebase, my check to see if I've got a float mismatch looks like this:

if expectedFloat != actualFloat && !(math.IsNaN(expectedFloat) && math.IsNaN(actualFloat)) {
    // error handling here...
}

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

No branches or pull requests

3 participants