Page view counter
Problem with Double
Last post 08-22-2008 8:15 AM by kwatts. 3 replies.
Sort Posts:
08-21-2008 4:43 PM
Problem with Double

 Hi,

Is there one reason which can explain the following statement :

Button b = new Button();

b.Width = 1.3;

Then

--> b.Width == 1.2999999523162842 !!! ??

 

Thanks

Thadeus

Loading...
Joined on 11-28-2007
Posts 4
08-21-2008 5:10 PM
Marked as Answer
Re: Problem with Double

The reason for this is the underlying representation of a double precision floating point number and how it is represented internally by the computer, in this case the CLR.  The value 1.2999999523162842 is very close to 1.3, so it should be good enough for most practical purposes.  But it's not good for all cases, such as with currency.

Here's a good wikipedia entry on the topic of floating point numbers:

http://en.wikipedia.org/wiki/Floating_point

 I hope that this helps!  Let me know if this is unclear or if this is causing a problem for you.  Otherwise, if I answered the question, please mark it as such.

Thanks! 

 


http://kenwatts.blogspot.com/


Please select "Mark as Answer" for posts that are helpful. Thanks!

kwatts

Loading...
Joined on 08-18-2008
Belchertown, MA
Posts 436
08-22-2008 4:04 AM
Re: Problem with Double

Thanks kwatts, so the problem comes from the Silverlight CLR ? Because with WPF, you do not have the same behavior (=> button.Width == 1.3)....

Thadeus

Loading...
Joined on 11-28-2007
Posts 4
08-22-2008 8:15 AM
Re: Problem with Double

My point is that it is not a problem.  It does seem strange that the behavior would be different in WPF than SIlverlight, but when you're playing with floating point numbers you can expect precision issues from time to time.

Is this behavior causing a problem for you?  If so, maybe you can elaborate on the issue and I can try to make some suggestions about what to do.

-Ken


http://kenwatts.blogspot.com/


Please select "Mark as Answer" for posts that are helpful. Thanks!

kwatts

Loading...
Joined on 08-18-2008
Belchertown, MA
Posts 436
Microsoft Communities