473,746 Members | 2,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Floating point problem ............... ......

Hi

please check ffollowing conditon
variable float1 and float2 holds user entered value.....

Answer=float1 * float2; //output must be 8.5

output must to be 8.5 but it has 8.500002, i am confused at this point
i used calculators and other calculation mathods to see the actual
value and thet give correct 8.5 with no other decimals......

i also know that to hold exact decimals of we use f for float and m for
decimal
but that is usefull at the time of assigning inline value that is

float fVar=100.1002f;

but what about if we want same kind of things in output/value generated
by variables?

please any one knows about it

Thanks
--------
Hitendra Patel

Nov 17 '05 #1
7 1973
Floating point numbers are just approximations to the number that you
actually want. Whenever you use them, you're going to get rounding errors.
If you don't want rounding errors, use the Decimal structure - that's a
fixed point numeric data type.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
"Hiten" <hi********@gma il.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Hi

please check ffollowing conditon
variable float1 and float2 holds user entered value.....

Answer=float1 * float2; //output must be 8.5

output must to be 8.5 but it has 8.500002, i am confused at this point
i used calculators and other calculation mathods to see the actual
value and thet give correct 8.5 with no other decimals......

i also know that to hold exact decimals of we use f for float and m for
decimal
but that is usefull at the time of assigning inline value that is

float fVar=100.1002f;

but what about if we want same kind of things in output/value generated
by variables?

please any one knows about it

Thanks
--------
Hitendra Patel

Nov 17 '05 #2
You seem not to understand the concept of floating point numbers.
Jon Skeet has a good article explaining "why are the arithmetics wrong",
refer to http://www.yoda.arachsys.com/csharp/floatingpoint.html or any
computer science book where you can learn about this issue

"Hiten" <hi********@gma il.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Hi

please check ffollowing conditon
variable float1 and float2 holds user entered value.....

Answer=float1 * float2; //output must be 8.5

output must to be 8.5 but it has 8.500002, i am confused at this point
i used calculators and other calculation mathods to see the actual
value and thet give correct 8.5 with no other decimals......

i also know that to hold exact decimals of we use f for float and m for
decimal
but that is usefull at the time of assigning inline value that is

float fVar=100.1002f;

but what about if we want same kind of things in output/value generated
by variables?

please any one knows about it

Thanks
--------
Hitendra Patel

Nov 17 '05 #3
declare your variable as type 'decimal'

A good computer science textbook on numerical representations wouldn't hurt
either.

Your computing hardware can only accurately represent specific values. It
gets as close as it can while still allowing the 'radix' to float (hence the
name). This allows the same variable to represent very large numbers (1.234
* 10^13) and very small numbers (0.34 * 10^-14).

Decimal types doesn't try. The decimal type represents a fixed number of
digits of accuracy. If the number cannot be represented by that fixed width
radix (as in astronomy calculations or algebraic formulas), then decimal
numbers aren't your answer. However, if you are doing money calculations,
or calculations where the rounding error is precalculated (such as
comparisons against a lookup table of some kind), then you are likely to be
much better off declaring decimal variables.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Hiten" <hi********@gma il.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Hi

please check ffollowing conditon
variable float1 and float2 holds user entered value.....

Answer=float1 * float2; //output must be 8.5

output must to be 8.5 but it has 8.500002, i am confused at this point
i used calculators and other calculation mathods to see the actual
value and thet give correct 8.5 with no other decimals......

i also know that to hold exact decimals of we use f for float and m for
decimal
but that is usefull at the time of assigning inline value that is

float fVar=100.1002f;

but what about if we want same kind of things in output/value generated
by variables?

please any one knows about it

Thanks
--------
Hitendra Patel

Nov 17 '05 #4
Tim Haughton <ti*********@gm ail.com> wrote:
Floating point numbers are just approximations to the number that you
actually want. Whenever you use them, you're going to get rounding errors.
If you don't want rounding errors, use the Decimal structure - that's a
fixed point numeric data type.


No, decimal is still floating point type - it's just a floating
*decimal* point rather than a floating *binary* point.

Floats/doubles are precise in the same way that decimals are - it's
just that the set of numbers which can be precisely represented is
different.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
it works with decimal i knew that there are problems with float &
double data type......

but to hold exact value with decimal with float,decimal we put f,m
after litral that is 100.99f & 100.99m but suppose i want some values
to be stored in variable and i want tha variable must hold exact value
as user enter

so there is any other way for it or i have to write my own logic or
class....

Nov 17 '05 #6
Nick Malik [Microsoft] <ni*******@hotm ail.nospam.com> wrote:
declare your variable as type 'decimal'

A good computer science textbook on numerical representations wouldn't hurt
either.

Your computing hardware can only accurately represent specific values. It
gets as close as it can while still allowing the 'radix' to float (hence the
name). This allows the same variable to represent very large numbers (1.234
* 10^13) and very small numbers (0.34 * 10^-14).

Decimal types doesn't try. The decimal type represents a fixed number of
digits of accuracy. If the number cannot be represented by that fixed width
radix (as in astronomy calculations or algebraic formulas), then decimal
numbers aren't your answer. However, if you are doing money calculations,
or calculations where the rounding error is precalculated (such as
comparisons against a lookup table of some kind), then you are likely to be
much better off declaring decimal variables.


I'm afraid there's a bit of confusion here.

Firstly, the radix itself doesn't float - it's 2 for double/float, and
10 for decimal. The radix *point* (i.e. the value of the exponent)
floats - and that's true for both decimal and float/double - they're
all floating points.

However, the range of the exponent in decimal is much smaller than the
range of the exponent in float/double.

In both cases, until you reach the extremes, the accuracy is
effectively fixed - 28 or 29 decimal digits for decimal, 15 or 16
decimal digits for double, and 7 decimal digits for float.

The most important difference between decimal and float/double,
however, is the base (the radix) - as decimal's radix is 10, it can
precisely store numbers which can be precisely represented as decimals
(subject to number of decimal places and scale). Compare this with
float/double, neither of which can store even 0.1 precisely, as that
cannot be precisely represented as a binary number.

See http://www.pobox.com/~skeet/csharp/decimal.html and
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 17 '05 #7
Hiten <hi********@gma il.com> wrote:
it works with decimal i knew that there are problems with float &
double data type......

but to hold exact value with decimal with float,decimal we put f,m
after litral that is 100.99f & 100.99m but suppose i want some values
to be stored in variable and i want tha variable must hold exact value
as user enter

so there is any other way for it or i have to write my own logic or
class....


You'll need to use Decimal.Parse, basically.

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

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

Similar topics

31
3677
by: JS | last post by:
We have the same floating point intensive C++ program that runs on Windows on Intel chip and on Sun Solaris on SPARC chips. The program reads the exactly the same input files on the two platforms. However, they generate slightly different results for floating point numbers. Are they really supposed to generate exactly the same results? I guess so because both platforms are supposed to be IEEE floating point standard (754?) compliant. ...
5
3750
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 using the Debug-Version of the program, but it fails (or leads to false results) if we are using the Release-Version. After a long debugging session we found out, that our program was working correctly, but the floating point processing...
687
23596
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't believe that. in pieces of the application where speed really matters you can still use "normal" functions or even static methods which is basically the same. in C there arent the simplest things present like constants, each struct and enum...
24
2244
by: j0mbolar | last post by:
C supports single precision floating point and double precision floating point but does it support fixed floating point? i've read that fixed floating point is more accurate than single precision floating point when dealing with dollars and cents.
7
3392
by: Vinoth | last post by:
I'm working in an ARM (ARM9) system which does not have Floating point co-processor or Floating point libraries. But it does support long long int (64 bits). Can you provide some link that would discuss about ways to emulate floating point calculations with just long int or long long int. For eg., if i've a formula X=(1-b)*Y + b*Z in floating point domain, i can calculate X with just long ints (but, some data may be lost in final division;...
15
3932
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this the EPSILON? I know in float.h a FLT_EPSILON is defined to be 10^-5. Does this mean that the computer cannot distinguish between 2 numbers that differ by less than this epsilon?
13
4150
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make sense to punch out from managed code to native code (I was using IJW) in order to do some amount of floating point work and, if so, what that certain amount of floating point work was approximately. To attempt to do this I made a program that...
4
2835
by: jacob navia | last post by:
Hi people I continue to work in the tutorial for lcc-win32, and started to try to explain the floating point flags. Here is the relevant part of the tutorial. Since it is a difficult part, I would like your expert advise before I publish any serious nonsense. Any comments are welcome, style, organization, hard errors, etc.
32
4115
by: ma740988 | last post by:
template <class T> inline bool isEqual( const T& a, const T& b, const T epsilon = std::numeric_limits<T>::epsilon() ) { const T diff = a - b; return ( diff <= epsilon ) && ( diff >= -epsilon ); } int main() { std::deque<double> pt ;
39
3568
by: rembremading | last post by:
Hi all! The following piece of code has (for me) completely unexpected behaviour. (I compile it with gcc-Version 4.0.3) Something goes wrong with the integer to float conversion. Maybe somebody out there understands what happens. Essentially, when I subtract the (double) function value GRID_POINT(2) from a variable which has been assigned the same value before this gives a non-zero result and I really do not understand why.
0
8800
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
9350
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...
0
9218
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
8229
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
6772
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
6060
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
4836
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3292
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
2765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.