-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Fixes #7228: Round °F temperature to one decimal place #7307
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -409,7 +409,7 @@ export function createElement< | |
| * @returns Degrees in Fahrenheit. | ||
| */ | ||
| export function cToF(celsius: number): number { | ||
| return celsius * (9 / 5) + 32; | ||
| return Math.round((celsius * (9 / 5) + 32 + Number.EPSILON) * 10) / 10; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this actually work? Certain numbers cannot be represented exactly using floating point (0.1 is one example); rounding doesn't change this. I would expect the problem is at the point of rendering it back to decimal.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please provide an example for this? I checked it with Math.round((0.1 + Number.EPSILON) * 10) / 10; # 0.1
Math.round((0.3 + Number.EPSILON) * 10) / 10; # 0.3
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I think I can replicate via Javascript console in Chrome: The problem is indeed fixed by your method, but actually it would be simpler to multiply by 9 and then divide by 5, rather than multiply by (9/5) which is an inexact floating point value; it appears that the error in (9/5) is magnified by multiplying by the degrees C value.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no guarantee that doing *9/5 will give the right answer though. I believe the correct solution is to convert the number to a string with the correct number of decimal places, using
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As you said, using only (1.005).toFixed(2) # 1.00
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes you're right: and yet: Floating point is broken, and Javascript especially so :-( However I think toFixed is OK here. I just want never to see a number with spurious 13 places of decimals. I don't mind if it's off by 0.1 degrees F, which it almost never will be anyway.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see what you meant. Floating point is evil :-( also (1).toFixed(1) // "1.0 "
Math.round((1 + Number.EPSILON) * 10) / 10; // 1this will add Anyway, I think my solution fix the bug we had and maybe different approch can be achived in other PR :) |
||
| } | ||
|
|
||
| /** | ||
|
|
||

Uh oh!
There was an error while loading. Please reload this page.