-
December 27th, 2013, 10:39 PM
#1
Comparison of 2 double values
Hi, ALL,
Today I got stumped by very interesting situation.
I have std::vector of players and each player have its own value. The value is of the type double, but the original value are of the "XX.00".
Now what I'm trying to do is to insert a player into this vector. For that I need to find a place and to find a place I need to compare player value.
So lets say I have a player in the vector whose value is 24.00 and I am trying to insert a player whose value is 25.00. To my surprise MSVC 2010 tells me that:
24.0000000000000 == 25.00000000000
I do aware that comparing double type is not going to work like this, but I would expect not to have such big of a difference. ;-)
Can someone please explain why this comparison is true and how to eliminate such thing?
Thank you.
-
December 28th, 2013, 05:23 AM
#2
Re: Comparison of 2 double values
It would help to advise if you posted the code you use.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 28th, 2013, 07:38 AM
#3
Re: Comparison of 2 double values
Code:
#include <stdio.h>
int main()
{
if (24.0000000000000 == 25.00000000000)
printf("24.0000000000000 == 25.00000000000");
else
printf("24.0000000000000 != 25.00000000000");
return 0;
}
Code:
J:\Temp\30>cl 30.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
30.cpp
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:30.exe
30.obj
J:\Temp\30>30.exe
24.0000000000000 != 25.00000000000
Last edited by Igor Vartanov; December 28th, 2013 at 07:40 AM.
Best regards,
Igor
-
December 28th, 2013, 09:16 AM
#4
Re: Comparison of 2 double values
If I had to guess, I'd say you're statement looks more like this
if(24.0000000000000 = 25.00000000000)
Whatever it is, the problem is in your code.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|