473,383 Members | 1,829 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,383 software developers and data experts.

Float/Double Substraction Bug ?

Hello,

I have the strange beeing :

float f1 = 108.88F;
float f2 = 111.11F;
float f3 = f1 - f2;
double d1 = 108.88;
double d2 = 111.11;
double d3 = d1 - d2;

f3 contains -2.23000336
d3 contains -2.230000000000004

instead of -2.23.
Am i missing something ?
Regards,
Cybertof.
Nov 15 '05 #1
4 1675
Hi cybertor,

This is normal behavious of binary based floating point variables.
They are only rounded to the value you want.
You might try using decimal variables instead.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com

"cybertof" <cy****************@gmx.net> wrote in message
news:MP***********************@msnews.microsoft.co m...
Hello,

I have the strange beeing :

float f1 = 108.88F;
float f2 = 111.11F;
float f3 = f1 - f2;
double d1 = 108.88;
double d2 = 111.11;
double d3 = d1 - d2;

f3 contains -2.23000336
d3 contains -2.230000000000004

instead of -2.23.
Am i missing something ?
Regards,
Cybertof.

Nov 15 '05 #2
Hi Miha !

How do you explain ?
Does it mean that with single & double, you always get calculation
errors ?

When can you use binary based floating point variables if you cannot
make correct calculations with them ?
Regards,
Cybertof.

In article <O1**************@TK2MSFTNGP11.phx.gbl>, "Miha Markic" <miha
at rthand com> says...
Hi cybertor,

This is normal behavious of binary based floating point variables.
They are only rounded to the value you want.
You might try using decimal variables instead.

Nov 15 '05 #3
This document may help explain it:

http://portal.acm.org/citation.cfm?i...TOKEN=87893990

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"cybertof" <cy****************@gmx.net> wrote in message
news:MP************************@msnews.microsoft.c om...
Hi Miha !

How do you explain ?
Does it mean that with single & double, you always get calculation
errors ?

When can you use binary based floating point variables if you cannot
make correct calculations with them ?
Regards,
Cybertof.

In article <O1**************@TK2MSFTNGP11.phx.gbl>, "Miha Markic" <miha
at rthand com> says...
Hi cybertor,

This is normal behavious of binary based floating point variables.
They are only rounded to the value you want.
You might try using decimal variables instead.

Nov 15 '05 #4
cybertof <cy****************@gmx.net> wrote:
How do you explain ?
Does it mean that with single & double, you always get calculation
errors ?
Often, yes - but not always.
When can you use binary based floating point variables if you cannot
make correct calculations with them ?


In fact, the calculations may well be performing without any error, but
with the initial values not being what you think they were.

See http://www.pobox.com/~skeet/csharp/floatingpoint.html for more
information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Sisyphus | last post by:
Hi, I have some software that does the following (in an attempt to determine whether the double x, can be represented just as accurately by a float): void test_it(double x) { float y = x;...
13
by: maadhuu | last post by:
hello , i would like to know as to why double is more efficient than float . thanking you, ranjan.
6
by: James Thurley | last post by:
According to the docs, floats are 32 bit and doubles are 64 bit. So using floats should be faster than using doubles on a 32 bit processor, and my tests confirm this. However, most of the Math...
5
by: Kubik | last post by:
Hi! Let's see, we got: float var=4.6f; //as we know 414/4.6 shoud be equal to 90 but Math.Ceiling(414/var) gives us 91 but (414/var).ToString() prints '90'.
2
by: julien | last post by:
Hi, I am using Sybase 12.5 dataserver and ASP.NET I am calling a stored procedure from my asp.net page, in this stored procedure, I have 2 float fields that are returned. One is directly taken...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
60
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
3
by: Arnie | last post by:
Folks, We ran into a pretty significant performance penalty when casting floats. We've identified a code workaround that we wanted to pass along but also was wondering if others had experience...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.