473,786 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.2300000000000 04

instead of -2.23.
Am i missing something ?
Regards,
Cybertof.
Nov 15 '05 #1
4 1690
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.microso ft.com...
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.2300000000000 04

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.micros oft.com...
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.co m>
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
2412
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; if(x == y) { printf("x can be represented precisely by a float\n"); }
13
3343
by: maadhuu | last post by:
hello , i would like to know as to why double is more efficient than float . thanking you, ranjan.
6
7276
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 methods deal with doubles and I'm having problems with casting them back to floats. For example: (double)0.1f = 0.10000000149011612 I don't need the accuracy of doubles, but the speed of floats would be benificial, but it seems I'm either...
5
2333
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
1407
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 from one float column of a table, and the other one is a substraction of 2 float columns of a table. The first field is all the time ok, whereas the calculated field is almost all the time wrong, that is it returns things like : 1,332294 - 1,334709...
6
7611
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, Karthi
60
7233
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
35999
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 problems elsewhere in another part of the system where that figure was used for some calculation and some eventual truncation led to the system going haywire. So my question is, given this code: int main() { float f = 59.89F;
3
10668
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 with this and if there is a better solution. -jeff
0
9650
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9962
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8992
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7515
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.