473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Equation 0.05 - 0.04 = 0.01 is not correct?! Help!!!

My code (Visual C++ Express 2008, CRT Console Application):
#include "iostream"

void main()
{
float rez = 0.05f - 0.04f;

printf_s("%f\n%i\n%i\n",
0.05f - 0.04f, // 0.010000, so that is 0.01
0.05f - 0.04f == 0.01f, // should be true, but...
0.05f - 0.04f == rez); // true
}
and I get this:
0.010000
0
1
Press any key to continue . . .
As you can see, my problem is 0.05f - 0.04f == 0.01f...
What project settings should I change to get correct results?
Sep 26 '09 #1
4 4165
newb16
687 512MB
Because 0.05 - 0.04 is not exactly 0.01 due to rounding - internal representation is binary and not decimal you can see in source code. And if you need to check if float/double equals exactly to some other number, you are doing it wrong.

Expand|Select|Wrap|Line Numbers
  1. float rez = 0.05f - 0.04f;
  2. float d1 = 0.01f;
  3. int *pp;
  4. pp = (int*)&rez;
  5. printf("%x\n", *pp);
  6. pp = (int*)&d1;
  7. printf("%x\n", *pp);
  8.  
if int is 32 bit, you can see that rez and d1 differ in some lowermost bit, although displayed with the same string.
Sep 27 '09 #2
Thank you.

Only one thing bad 1% more complex code...

You said that string representations will be equal... I'll use that...
Sep 27 '09 #3
Banfa
9,065 Expert Mod 8TB
If you are using a fixed number of decimal places then most programmers use integers.

That is if I am holding values in the range -120.000 to 120.000 instead of using a float use an 32 bit integer and times the value by 1000 to hold values in the range -120000 to 120000. Do all you calculations in integers, being careful to take account of integer wrap round, just put in the decimal point for display to the user.
Sep 27 '09 #4
donbock
2,426 Expert 2GB
You should read What Every Computer Scientist Should Know About Floating-Point Arithmetic.

This and other helpful links are in the PLEASE READ FIRST thread.
Sep 28 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Al, Cambridge, UK | last post by:
I thought I'd share this, since it cost me 2.5 days... When running Visual Studio .NET 2003 as a Power User, if I pressed F1 for context-sensitive help, I either (consistently for particular...
16
by: Fahad | last post by:
I am a newcommer in c world, I am making a tiny program on Mathematics. I have one problem if somebody enter his name, "i am able to make the first letter uppercase but my problem how can i make...
38
by: Red Dragon | last post by:
I am self study C student. I got stuck in the program below on quadratic equation and will be most grateful if someone could help me to unravel the mystery. Why does the computer refuse to execute...
4
by: Bob Homes | last post by:
In VB6, I used a system, which I loved, whereby I assigned a "helpId" to each menu item; that way, you could rest the cursor on the item (without actually running it) and then press F1 to get...
10
by: David Thielen | last post by:
Hi; I have help html pages for each page of my ASP.NET webapp. So for the page datasource.aspx, I have help\datasource.htm. Bu what I want when the hyperlink is clicked, for it to look for the...
1
by: G Gerard | last post by:
Hello I have written some help files (MyHelp) using Html Help Workshop version 4.74.8702 When I launch MyHelp.chm from a MSACCESS 2000 application the MyHelp help window opens displaying the...
2
by: mayur123creation | last post by:
Hi to All Experts.... I am new in DB side. I need to update all rows for column (fees) in table (MarketValue). This table has around 1500 rows. TB : MarketValue has following colums id - int...
3
by: Philippe HALET | last post by:
Dear all, I have an application written in C#. The main form contains two areas: the first one for data input and results, the second one for informations. In this last area I would like to...
10
by: Constantine AI | last post by:
Hi i am having a little problem with an equation function that was created from all your help previously. The function works fine itself but with a small glitch within it. Here is the function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
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...
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
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,...
0
tracyyun
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.