473,413 Members | 2,051 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,413 software developers and data experts.

floating point error

hi experts,

i am facing trouble with float data types.

when i try to parse a value to float it gives me diffrent value.

e.g float test = (float.Parse("149.128156"));

gives me test=149.128159

i am very fed up of this.. pls help me out ...

i want the exact values of what i gave as input.

thanks in advance
Aug 31 '09 #1
6 2434
tlhintoq
3,525 Expert 2GB
More about floats than you would want to know:
http://docs.sun.com/source/806-3568/ncg_goldberg.html

But I do notice that a double works fine for your example:
Expand|Select|Wrap|Line Numbers
  1.             float Test = float.Parse("149.128156");// Breakpoint here and walk through looking at Locals values
  2.             float Bob = Test;// Yep, Bob is wrong here
  3.             MessageBox.Show("Parse",Test.ToString());
  4.             double Yogi = double.Parse("149.128156");
  5.             double booboo = Yogi;// But Yogi is right here
  6.             MessageBox.Show("Double", Yogi.ToString());
  7.  
Aug 31 '09 #2
thanks tlhintoq fro your response.

i know that i can get my values by using double..

but i have a problem in which i must have to use float.

so pls give me some suggestion by which i can get perfect values in float
Aug 31 '09 #3
tlhintoq
3,525 Expert 2GB
Since you know parsing to a double gets you the right value, I would experiment from that. Parse to a double, then seat a float equal to the double; and so on. See if some combination of those get you a usable float as the final step.
Aug 31 '09 #4
i do not get you .. can you pls little more specific with a small example/..

thanks in advance
Aug 31 '09 #5
tlhintoq
3,525 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. double Yogi = double.Parse("149.128156");
  2. float bob = Yogi
  3. // the Double does the parsing accurately
  4. // then gives it to a float for you to use elsewhere per your need to use a float.
  5.  
Aug 31 '09 #6
thanks for your response but...
its not working b/c there is no implicit conversion from double to float .in c#
we have to write as follows
float bob =(float) Yogi;

then it again gives me 149.128159

so still i have same problem.
Sep 1 '09 #7

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

Similar topics

4
by: Dave | last post by:
Hi folks, I am trying to develop a routine that will handle sphere-sphere and sphere-triangle collisions and interactions. My aim is to develop a quake style collision engine where a player can...
687
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...
3
by: Mark L Pappin | last post by:
<puts on Compiler Vendor hat> I've recently discovered that our compilers don't make any attempt to handle floating point overflow in add/subtract/ multiply/divide, with the result that...
15
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...
4
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...
4
by: alex | last post by:
hi friends ... i am facing a problem while detecting floating point operations in my project, please help me. i want to find out the places in my C/C++ project where i am doing floating...
1
by: Shhnwz.a | last post by:
Hi, I have a problem regarding handling floating point error, specially Denormalisation error. I want to know is there any trick or technique to find bit length of the resultant before using and...
15
by: Mukesh_Singh_Nick | last post by:
Why does floating point have a rounding error? How to work around it? For example, the following: flaot f = 1234.12345678F; printf("%2f\n", f) //prints 1234.123413 and
5
by: Keflavich | last post by:
Hey, I have a bit of code that died on a domain error when doing an arcsin, and apparently it's because floating point subtraction is having problems. I know about the impossibility of storing...
7
by: ma740988 | last post by:
Consider the equation (flight dynamics stuff): Yaw (Degrees) = Azimuth Angle(Radians) * 180 (Degrees) / 3.1415926535897932384626433832795 (Radians) There's a valid reason to use single...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...
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...
0
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...

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.