For Programmers: Free Programming Magazines  


Home > Archive > C# > August 2006 > BUG!! with Single and Math.Round









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author BUG!! with Single and Math.Round
Wayne Fontes

2005-05-26, 3:57 am

I think I found a bug. If not, I really need someone to explain this
to me.

I have a method that needs to return a Single. I need to round the
value to 2 decimals and this has always worked fine. However, the code
below will always return the Single variable "c" as the original value
of variable "st". I have tried this with decimals, doubles and many
variations only to get the same result. However, if I change the value
of "st" to be one digit less (20294), it works. When I assign the
value to v and cast to a Single, how does it know the original value
before it was rounded? Shouldn't those primitive types have their own
values that are totally disconnected from the original variable "st"?

double st = 120294.719F;
double v = Math.Round(st,2);
Single c = Convert.ToSingle(v);
Console.WriteLine("Value: {0}",c);

thkoe002

2006-08-06, 5:31 pm

quote:
Originally posted by Wayne Fontes
I think I found a bug. If not, I really need someone to explain this
to me.

I have a method that needs to return a Single. I need to round the
value to 2 decimals and this has always worked fine. However, the code
below will always return the Single variable "c" as the original value
of variable "st". I have tried this with decimals, doubles and many
variations only to get the same result. However, if I change the value
of "st" to be one digit less (20294), it works. When I assign the
value to v and cast to a Single, how does it know the original value
before it was rounded? Shouldn't those primitive types have their own
values that are totally disconnected from the original variable "st"?

double st = 120294.719F;
double v = Math.Round(st,2);
Single c = Convert.ToSingle(v);
Console.WriteLine("Value: {0}",c);


Just wanted to add that I have the same problem. This is very confusing to me, as Math.Round() works fine in all my other projects, but currently, for example the coee below returns 43.234539031982422 instead of 43.23453.. any ideas?
I use c# express 2005, the application makes use of DirectX and float variables (I dont think that it matters, but this is the only difference to all other working projects that I can think of)...

double a = Math.Round(43.234534534, 5);
//same thing happens with
double a = 43.234534534;
a = Math.Round(a, 5);
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com