Skip to content
Discussion options

You must be logged in to vote

I assigned 1.0000000000000002220446049250313080847263336181640625 to a double then converted it to a string with format "r" and get 1.0000000000000002 which as you can see is not the same value. If I format it as "G999" I get the full value back.

This is not how you compare equality of floating-point values. As you can see from the below, these parse to the same value and have the same bitwise representation.


This is for the same reason you cannot do something like 1.0 vs 1 vs 1.000 vs 10e-1 and so on. They all represent the same value, even though the string representation differs. As such, you must parse the string to a double and check the double value represented is the same.

Almo…

Replies: 1 comment 12 replies

Comment options

You must be logged in to vote
12 replies
@The-Futurist
Comment options

@tannergooding
Comment options

Answer selected by The-Futurist
@The-Futurist
Comment options

@Clockwork-Muse
Comment options

@The-Futurist
Comment options

@Clockwork-Muse
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
3 participants