What is going on with MS VC compiler team? The following trivial program
will give wrong answer of 9 instead of 10. Perhaps MS is spending too much
time on managed world and forget about the real world. If we can not
multiply two numbers and get a right answer, what good is it ?
// test_int.cpp : Defines the entry point for the console application.
// the program will give wrong answer for ftemp=0.01,0.02,0.03,0.04
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int itemp;
float ftemp = 0.01f;
cout << ftemp*1000.f << endl;
cout << int(ftemp*1000.f) << endl;
cin >> itemp;
return 0;
} 6 2097
LTO wrote: What is going on with MS VC compiler team? The following trivial program will give wrong answer of 9 instead of 10. Perhaps MS is spending too much time on managed world and forget about the real world. If we can not multiply two numbers and get a right answer, what good is it ?
Or perhaps you're spending too little time understanding what the code you
write means.
The number 0.01 cannot be represented accurately in floating point (no
negative power of 10 can be). So instead of 0.01, it's really 0.0099999...,
so when you multiply it by 1000 (which is represented accurately), the
result is 9.99999..., which is 9 when it's truncated to an integer. If you
want rounding, you have to ask for it. // test_int.cpp : Defines the entry point for the console application. // the program will give wrong answer for ftemp=0.01,0.02,0.03,0.04
#include "stdafx.h" #include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) { int itemp; float ftemp = 0.01f; cout << ftemp*1000.f << endl; cout << int(ftemp*1000.f) << endl;
cout << int(ftemp * 1000.0f + 0.5) << endl;
cin >> itemp; return 0; }
-cd
Sorry! I didn't mean to be rude. I just got a litle bit upset because a
whole afternoon was wasted because of this. Anyway, the code can be modified
to make it work for all case:
cout << int((ftemp*1000.f)+0.1f) << endll;
Thanks for pointing out my short sightedness!
"Carl Daniel [VC++ MVP]" <cp******@nospam.mvps.org> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... LTO wrote: What is going on with MS VC compiler team? The following trivial program will give wrong answer of 9 instead of 10. Perhaps MS is spending too much time on managed world and forget about the real world. If we can not multiply two numbers and get a right answer, what good is it ? Or perhaps you're spending too little time understanding what the code you write means.
The number 0.01 cannot be represented accurately in floating point (no negative power of 10 can be). So instead of 0.01, it's really
0.0099999..., so when you multiply it by 1000 (which is represented accurately), the result is 9.99999..., which is 9 when it's truncated to an integer. If
you want rounding, you have to ask for it.
// test_int.cpp : Defines the entry point for the console application. // the program will give wrong answer for ftemp=0.01,0.02,0.03,0.04
#include "stdafx.h" #include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) { int itemp; float ftemp = 0.01f; cout << ftemp*1000.f << endl; cout << int(ftemp*1000.f) << endl;
cout << int(ftemp * 1000.0f + 0.5) << endl;
cin >> itemp; return 0; }
-cd
Floating point results are never precise, except by coincidence. Some
numbers that can't be represented exactly will actually be slightly larger
than the "true" value, while others will be slightly smaller. Depending on
the exact calculation you're performing, the slightly larger ones may lead
to the "correct" result, while for a different calculation, the slightly
smallers ones might give the "correct" answer.
The thing to realize is that all floating point is inexact. You absolutely
must think about and control rounding and precision issues in every floating
point expression you write.
-cd
LTO wrote: Ok, the number 0.01 can not be represented precisely in floating point. Then why does the compiler generate program which produces precise answers to ftemp = 0.1 and 0.001? Thanks!
"Carl Daniel [VC++ MVP]" <cp******@nospam.mvps.org> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl... LTO wrote: What is going on with MS VC compiler team? The following trivial program will give wrong answer of 9 instead of 10. Perhaps MS is spending too much time on managed world and forget about the real world. If we can not multiply two numbers and get a right answer, what good is it ?
Or perhaps you're spending too little time understanding what the code you write means.
The number 0.01 cannot be represented accurately in floating point (no negative power of 10 can be). So instead of 0.01, it's really 0.0099999..., so when you multiply it by 1000 (which is represented accurately), the result is 9.99999..., which is 9 when it's truncated to an integer. If you want rounding, you have to ask for it.
// test_int.cpp : Defines the entry point for the console application. // the program will give wrong answer for ftemp=0.01,0.02,0.03,0.04
#include "stdafx.h" #include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) { int itemp; float ftemp = 0.01f; cout << ftemp*1000.f << endl; cout << int(ftemp*1000.f) << endl;
cout << int(ftemp * 1000.0f + 0.5) << endl;
cin >> itemp; return 0; }
-cd
"LTO" <lt*@ieee.org> skrev i meddelandet
news:OC**************@tk2msftngp13.phx.gbl... Sorry! I didn't mean to be rude. I just got a litle bit upset
because a whole afternoon was wasted because of this. Anyway, the code can be
modified to make it work for all case: cout << int((ftemp*1000.f)+0.1f) << endll;
No, this doesn't work for all cases, just for your test case. The
general solution is to add 0.5 (or a value half the last digit you are
rounding to), like Carl showed you. Consider these cases:
trunc(1.4 + 0.5) == trunc(1.9) == 1
trunc(1.6 + 0.5) == trunc(2.1) == 2
Less than x.5 is rounded to x
Greater or equal to x.5 is rounded to x+1
Bo Persson bo**@telia.com
> No, this doesn't work for all cases, just for your test case. The general solution is to add 0.5 (or a value half the last digit you are rounding to), like Carl showed you. Consider these cases:
trunc(1.4 + 0.5) == trunc(1.9) == 1 trunc(1.6 + 0.5) == trunc(2.1) == 2
Less than x.5 is rounded to x Greater or equal to x.5 is rounded to x+1
Now, the big question is, what will be the result of trunc(x.5 + 0.5)? :)
OK, then why does the following give the answers 9 and 10
for what appears to be the same value?
int itemp;
float ftemp = 0.01f;
CString temp;
temp.Format ("%f", ftemp*1000.f);
SetDlgItemText(IDC_RESULT, temp);
float t = ftemp*1000.f;
SetDlgItemInt ( IDC_RESULT2, int(t));
SetDlgItemInt ( IDC_RESULT3, int(ftemp*1000.f));
the results in the edit boxes are
10.00000000
10
9 -----Original Message----- Floating point results are never precise, except by
coincidence. Somenumbers that can't be represented exactly will actually
be slightly largerthan the "true" value, while others will be slightly
smaller. Depending onthe exact calculation you're performing, the slightly
larger ones may leadto the "correct" result, while for a different
calculation, the slightlysmallers ones might give the "correct" answer.
The thing to realize is that all floating point is
inexact. You absolutelymust think about and control rounding and precision
issues in every floatingpoint expression you write.
-cd
LTO wrote: Ok, the number 0.01 can not be represented precisely in
floating point. Then why does the compiler generate program
which produces precise answers to ftemp = 0.1 and 0.001? Thanks!
"Carl Daniel [VC++ MVP]" <cp******@nospam.mvps.org>
wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl... LTO wrote: What is going on with MS VC compiler team? The
following trivial program will give wrong answer of 9 instead of 10.
Perhaps MS is spending too much time on managed world and forget
about the real world. If we can not multiply two numbers and get a
right answer, what good is it ?
Or perhaps you're spending too little time
understanding what the code you write means.
The number 0.01 cannot be represented accurately in
floating point (no negative power of 10 can be). So instead of 0.01,
it's really 0.0099999..., so when you multiply it by 1000 (which
is represented accurately), the result is 9.99999..., which is 9 when
it's truncated to an integer. If you want rounding, you
have to ask for it.
// test_int.cpp : Defines the entry point for the
console application. // the program will give wrong answer for ftemp=0.01,0.02,0.03,0.04
#include "stdafx.h" #include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) { int itemp; float ftemp = 0.01f; cout << ftemp*1000.f << endl; cout << int(ftemp*1000.f) << endl;
cout << int(ftemp * 1000.0f + 0.5) << endl;
cin >> itemp; return 0; }
-cd
. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Tom Lee |
last post by:
Hi,
I'm new to .NET 2003 compiler. When I tried to compile my
program using DEBUG mode, I got the following errors in the
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7
\include\xdebug...
|
by: Anton Noll |
last post by:
We are using Visual Studio 2003.NET (C++) for the development
of our software in the fields digital signal processing and
numerical acoustics.
One of our programs was working correctly if we are...
|
by: Gordon |
last post by:
Hello there.
I am attempting to move some working code to VC++ .Net2003.
The problem I have is with a structure (base class CObList)
that I use for a simple linked. It will not even compile
in...
|
by: Michael |
last post by:
i experience slower compile times with VC++ 2003 compared to VC+6.0.
Anyone experiencing the same?
Should that be expected?
This ineed matters, when total compilation time is > 1h and you have to...
|
by: Ger |
last post by:
I am having problems using VB.Net's Management base object on a
machine hosting Windows Server 2003. I am trying to set file
permissions from a Windows Service. These files may be loacted on a...
|
by: Arnaud Miege |
last post by:
Hi,
I am going through a C tutorial and would like to compile a simple C program
using Microsoft Visual Studio .NET 2003. I can open my *.c file but I can't
figure out a way to compile it.
If...
|
by: Mell via AccessMonster.com |
last post by:
Is there a way to find out where an application was created from?
i.e. - work or home
i.e. - if application sits on a (work) server/network, the IT people know the
application is sitting...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| | |