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

cgen: fix assert rvalue printing for float numbers #21538

Merged
merged 3 commits into from
May 23, 2024

Conversation

felipensp
Copy link
Member

@felipensp felipensp commented May 20, 2024

Fix #21536

f := 0.2
assert f == 0.3

@JalonSolov
Copy link
Contributor

The problem with IEEE-754 floats is they are not guaranteed to be exact to any particular number of digits. Assert for floats will only ever accidentally match exactly.

It might be better to simply not allow == checks on pure floats... only <, >, >=, etc. Inexact matches.

@felipensp felipensp changed the title cgen: fix assert for float numbers cgen: fix assert rvalue printing for float numbers May 20, 2024
@felipensp
Copy link
Member Author

The problem with IEEE-754 floats is they are not guaranteed to be exact to any particular number of digits. Assert for floats will only ever accidentally match exactly.

It might be better to simply not allow == checks on pure floats... only <, >, >=, etc. Inexact matches.

Maybe my example was not good. But the point is about the *unknown value* print instead of the real float number in comparison.

@Delta456
Copy link
Member

The problem with IEEE-754 floats is they are not guaranteed to be exact to any particular number of digits. Assert for floats will only ever accidentally match exactly.

It might be better to simply not allow == checks on pure floats... only <, >, >=, etc. Inexact matches.

If == is allowed for all other operations of float then why disallow for assert statements? If you are going to disallow equality operations for floats, may just disallow it completely.

@JalonSolov
Copy link
Contributor

JalonSolov commented May 20, 2024

Well, some people have hopes... the problem with allowing == for asserts is you will get a lot of "false negatives" (as in, they won't match when you think they should). In regular code, well, you get what you get, and people have been dealing with it for years.

@JalonSolov
Copy link
Contributor

For example, a very simple test might always work...

pub fn test_format() {
        f := 0.2
        assert f == 0.2
}

However, if you did several calculations before the assert, you might instead end up with

  > assert f == f64(0.2)
     Left value: 0.2
    Right value: 0.200001

@Delta456
Copy link
Member

I can work with a notice alert. Not an error.

@JalonSolov
Copy link
Contributor

That would be fine, I suppose. The problem is that many (if not most) expect floats to act the same as ints (as in, precision the same, no round-off errors, etc.), but they don't.

And when they don't, we get questions about why they don't.

So letting them know up front that using == on floats may not always work would be a good thing.

@medvednikov
Copy link
Member

I remember I wanted to ban float1 == float2 way back, but there was a discussion with some arguments against this.

Can't find it now.

I think it makes sense.

@felipensp felipensp marked this pull request as ready for review May 22, 2024 12:37
Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

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

Excellent work.

@spytheman
Copy link
Member

The PR is only slightly related to x == 0.11111 etc.

It fixes printing of floating point values for all assertions, including for assert f > x, see for example:
image

fn test_abc() {
	f := 0.0
	assert f > 0.3
}

@spytheman spytheman merged commit 2b1be90 into vlang:master May 23, 2024
69 checks passed
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

Successfully merging this pull request may close these issues.

v test does not print float value
5 participants