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

Assert: check both relative and absolute error when comparing floats #68

Merged
merged 2 commits into from
Jan 7, 2014

Conversation

JanTvrdik
Copy link
Contributor

As it turns out you actually need to check both relative and absolute error 😸

@milo
Copy link
Member

milo commented Jan 6, 2014

What about small floats with opposite sign like array(-1e-14, 1e-14). Can be considered as equal?

@hrach
Copy link

hrach commented Jan 6, 2014

@milo why?

@milo
Copy link
Member

milo commented Jan 6, 2014

@hrach Sorry, I wrote it wrong. I mean, proposed implementation allows such equality and I think it should not.

Moreover, on PHP 5.3.6 it is failing: INF is equal to INF and NON is equal to NON.

I do not want to be such a bad guy, but it needs just small update :)

@JanTvrdik
Copy link
Contributor Author

What about small floats with opposite sign like array(-1e-14, 1e-14). Can be considered as equal?

When you put it like this, it it hard to decide. But the idea for Assert::equal when comparing floats is that it should ignore „float imperfection“.

The following should be equal if float was "perfect", therefore we intentionally allow numbers like -1e-14, 1e-14 to be equal.

$float1 = 1 / 3;
$float2 = 1 - 2 / 3;
Assert::equal($float1 - $float2, $float2 - $float1);

@JanTvrdik
Copy link
Contributor Author

I'll make the example even better – why should the following have different results?

Assert::equal($float1 - $float2 + 123, $float2 - $float1 + 123);
Assert::equal($float1 - $float2,       $float2 - $float1);

@JanTvrdik
Copy link
Contributor Author

And reason why it fails on PHP < 5.4.0 http://3v4l.org/XJdZW

@JanTvrdik
Copy link
Contributor Author

I changed expectation to make INF equal INF because http://3v4l.org/UICCp

@milo
Copy link
Member

milo commented Jan 7, 2014

I'll make the example even better...

OK, it makes sense.

I changed expectation to make INF equal INF...

As I rememer university math, two infinities cannot be equal so easily, it depends how quick they are coming to infinity, so I would prefer FALSE. But it is about PHP language vs. math.

If no one protests, I would merge that.

@JanTvrdik
Copy link
Contributor Author

Agreed on your opinion about infinity comparison, however Assert::same uses === and Assert::equal must be in this matter consistent with Assert::same.

milo added a commit that referenced this pull request Jan 7, 2014
Assert: check both relative and absolute error when comparing floats
@milo milo merged commit 271580b into nette:master Jan 7, 2014
@JanTvrdik JanTvrdik deleted the rel_abs_eps branch January 8, 2014 13:06
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.

None yet

3 participants