473,666 Members | 2,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem in Float Arithmetic

Hi All,

I am getting exponent value when doing float arithmetic in C++.
Instead of that I need accurate value.

float amount = 0.0f;
float x = 0.99999976f;
amount= 1.0f - x;

I am getting amount as 2.3841858e-007 instead of 0.00000024
Can anyone tell what will
be the solution for this problem???

Note : I am getting correct value in C#.Net but not in C++

Thanks in advance.

Regards,
K

Jun 27 '08 #1
5 2619
On Mon, 23 Jun 2008 04:51:40 -0700, Selvam wrote:
Hi All,

I am getting exponent value when doing float arithmetic in C++. Instead
of that I need accurate value.

float amount = 0.0f;
float x = 0.99999976f;
amount= 1.0f - x;

I am getting amount as 2.3841858e-007 instead of 0.00000024 Can anyone
tell what will
be the solution for this problem???

Note : I am getting correct value in C#.Net but not in C++
See this FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-29.16

Regards,

--
Lionel B
Jun 27 '08 #2
Selvam <ka***********@ gmail.comwrites :
Hi All,

I am getting exponent value when doing float arithmetic in C++.
Instead of that I need accurate value.

float amount = 0.0f;
float x = 0.99999976f;
amount= 1.0f - x;

I am getting amount as 2.3841858e-007 instead of 0.00000024
Have a look at:
"What Every Computer Scientist Should Know About Floating-Point Arithmetic"
http://focus.hut.fi/docs/WorkShop/co...berg1.doc.html
http://docs-pdf.sun.com/800-7895/800-7895.pdf
http://portal.acm.org/citation.cfm?id=103163
Can anyone tell what will
be the solution for this problem???
The bug you made is here: float amount;
^^^^^ ^^^^^^

Amounts ARE NOT floating point numbers. You should NEVER use
float(-ing point) numbers in financial applications.

If you want exact arithmetic, use integers (and rationals or
fixed-point numbers). http://gmplib.org/

Note : I am getting correct value in C#.Net but not in C++
Only because C#{float} == C++{double}.
Try:

float weight = 0.0f;
float x = 0.9999999999999 99976f;
weight= 1.0f - x;

in C#. (Notice how I changed the application domain, from amounts to
weight, to make it a correct program).

--
__Pascal Bourguignon__
Jun 27 '08 #3
On Mon, 23 Jun 2008 14:15:22 +0200, Pascal J. Bourguignon wrote:
Selvam <ka***********@ gmail.comwrites :
>Hi All,

I am getting exponent value when doing float arithmetic in C++. Instead
of that I need accurate value.

float amount = 0.0f;
float x = 0.99999976f;
amount= 1.0f - x;

I am getting amount as 2.3841858e-007 instead of 0.00000024
[...]
The bug you made is here: float amount;
^^^^^ ^^^^^^

Amounts ARE NOT floating point numbers. You should NEVER use float(-ing
point) numbers in financial applications.
Where did the OP say "financial application"? There are amounts of other
stuff besides money, surely ;-)

Aside from which, I'm not convinced that's realistic advice (I once
programmed for a financial institution where we worked quite happily in
floating point - paying very careful attention to precision and rounding,
of course).
If you want exact arithmetic, use integers (and rationals or fixed-point
numbers). http://gmplib.org/
Maybe - but seeing as we don't know what the problem domain of the OP is,
this may not be the way to go.
>Note : I am getting correct value in C#.Net but not in C++

Only because C#{float} == C++{double}. Try:

float weight = 0.0f;
float x = 0.9999999999999 99976f;
weight= 1.0f - x;

in C#. (Notice how I changed the application domain, from amounts to
weight, to make it a correct program).
You've changed the application domain? You must be psychic. Personally
I've no idea what the OP's application domain might be.

--
Lionel B
Jun 27 '08 #4
Selvam wrote:
Instead of that I need accurate value.
Then don't use floating point numbers.
float amount = 0.0f;
float x = 0.99999976f;
amount= 1.0f - x;

I am getting amount as 2.3841858e-007 instead of 0.00000024
Using 'double' instead of 'float' may decrease the rounding errors,
but probably won't remove them.
Jun 27 '08 #5
On Jun 23, 1:51 pm, Selvam <kalaiselva...@ gmail.comwrote:
I am getting exponent value when doing float arithmetic in
C++. Instead of that I need accurate value.
I'm not sure what you mean by "accurate value". You're
probablyl getting a reasonably accurate value in the results,
but just not formatting it correctly.
float amount = 0.0f;
float x = 0.99999976f;
amount= 1.0f - x;
I am getting amount as 2.3841858e-007 instead of 0.00000024
Which is probably more accurate. (Note that despite what you
wrote, you certainly never had a float value of 0.99999976.) Of
course, if what you want is the value rounded to 8 points after
the decimal, then you have to tell the computer to output it
like that:
std::cout.setf( std::ios::fixed , std::ios::float field ) ;
std::cout.preci sion( 8 ) ;
std::cout << x ;
Typically, of course, you'll wrap those first two statements in
some sort of application specific manipulator, and write
something like:

std::cout << appliFmt << x ;

(Also: float is only good for six or seven decimal digits
accuracy; you probably want to consider double.)
Can anyone tell what will be the solution for this problem???
Note : I am getting correct value in C#.Net but not in C++
I'm pretty sure that you're getting the same results in both
cases. But the default formatting isn't necessarily the same.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #6

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

Similar topics

6
2657
by: asmirnov1234567890 | last post by:
Hi my python 2.3.4 for windows refuse to execute line float("NaN"). It says: >>> float("NaN") Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: invalid literal for float(): NaN
3
146600
by: David Sharp | last post by:
I'm trying to find a way to format a FLOAT variable into a varchar in SQL Server 2000 but using CAST/CONVERT I can only get scientific notation i.e. 1e+006 instead of 1000000 which isn't really what I wanted. Preferably the varchar would display the number to 2 decimal places but I'd settle for integers only as this conversion isn't business critical and is a nice to have for background information. Casting to MONEY or NUMERIC before...
5
4544
by: Yodai | last post by:
Hi all! I have an int that comes with a value like 0x07fa and I have to turn it into a float value of 204.2 decimal to display it.... if I try to divide it by 10 I get bogus numbers. I presume I have to make it decimal before calculating, but I am not sure about it. Any light upon this simple (or it seems to me so, at least) begginner problem? TIA..
54
8345
by: Andy | last post by:
Hi, I don't know if this is the correct group to post this, but when I multiply a huge floating point value by a really small (non-zero) floating point value, I get 0 (zero) for the result. This creates a big hole in a 32-bit timer routine I wrote. Questions. 1. Why does this happen? 2. Is there C macros/functions I can call to tell me when two non-zero numbers are multiplied and the
18
1736
by: Manohar S | last post by:
It is a problem related to printf, and buffering to the printf statements Look through this program. main() { int pid; int loop,max=3; for(loop = 0; loop <max;loop++) { pid = fork();
1
3869
by: Shreyas Kulkarni | last post by:
hi there, recently i have got a problem regarding calculation of sum of digits in a floating point or precision number. the weird behaviour of compiler/language is preventing me from calculating the sum of all digits. the compiler doesnt store the number as given by the user. some of the precision digits are lost or changed (at will!). so by any method, i m unable to reliably calculate the sum of provided number; except i input the...
60
7197
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
27
2859
by: galt_57 | last post by:
I need to do just a few multiplies of long integers and have a divide by ten. Can I avoid using floats? Can I use two longs as a 64 bit value somehow? Thanks.
116
35836
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;
0
8352
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8863
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8780
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
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
7378
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...
0
5661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4192
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...
0
4358
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2765
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

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.