By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,143 Members | 1,855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,143 IT Pros & Developers. It's quick & easy.

what's wrong with float ?

P: n/a
create a J# windows applicaion.add a button , a label and two textboxes to
form and paste this code into the buttons event handler:

private void button1_Click(Object sender, System.EventArgs e)
{
label1.set_Text(Float.toString(
Float.parseFloat(textBox1.get_Text())
*
Float.parseFloat(textBox2.get_Text())
));
}

when you run the project and test it with 3 and 6.7 ; you get freaking
result of "20.0999985" in the label instead of "20.1" .if try 3 and
6.71,you'll get "20.1300011" instead of "20.13" .

what the hell is wrong with J# float numbers?
Dec 9 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Sasi <Sa**@discussions.microsoft.comwrote:
create a J# windows applicaion.add a button , a label and two textboxes to
form and paste this code into the buttons event handler:

private void button1_Click(Object sender, System.EventArgs e)
{
label1.set_Text(Float.toString(
Float.parseFloat(textBox1.get_Text())
*
Float.parseFloat(textBox2.get_Text())
));
}

when you run the project and test it with 3 and 6.7 ; you get freaking
result of "20.0999985" in the label instead of "20.1" .if try 3 and
6.71,you'll get "20.1300011" instead of "20.13" .

what the hell is wrong with J# float numbers?
Nothing. What's wrong is your expectations of binary floating point
numbers.

See http://pobox.com/~skeet/csharp/floatingpoint.html

Note that it has little to do with J#, either - the following C#
application (complete - no need for a UI etc) demonstrates the same
behaviour.

using System;

class Test
{
static void Main()
{
float f1 = 3.0f;
float f2 = 6.7f;

float f3 = f1*f2;

Console.WriteLine (f3.ToString("r"));
}
}

Without the "r" format specifier, you'll *think* you've got the
"correct" answer because by default .NET doesn't show you all the
information it's got; the Java formatting defaults to a more precise
form.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 9 '07 #2

P: n/a
cool.
I ran your sample and noticed that if I omit "r" modifier i'll get the
result which is desired for me.
how can i do the same in J# ?

"Jon Skeet [C# MVP]" wrote:
Sasi <Sa**@discussions.microsoft.comwrote:
create a J# windows applicaion.add a button , a label and two textboxes to
form and paste this code into the buttons event handler:

private void button1_Click(Object sender, System.EventArgs e)
{
label1.set_Text(Float.toString(
Float.parseFloat(textBox1.get_Text())
*
Float.parseFloat(textBox2.get_Text())
));
}

when you run the project and test it with 3 and 6.7 ; you get freaking
result of "20.0999985" in the label instead of "20.1" .if try 3 and
6.71,you'll get "20.1300011" instead of "20.13" .

what the hell is wrong with J# float numbers?

Nothing. What's wrong is your expectations of binary floating point
numbers.

See http://pobox.com/~skeet/csharp/floatingpoint.html

Note that it has little to do with J#, either - the following C#
application (complete - no need for a UI etc) demonstrates the same
behaviour.

using System;

class Test
{
static void Main()
{
float f1 = 3.0f;
float f2 = 6.7f;

float f3 = f1*f2;

Console.WriteLine (f3.ToString("r"));
}
}

Without the "r" format specifier, you'll *think* you've got the
"correct" answer because by default .NET doesn't show you all the
information it's got; the Java formatting defaults to a more precise
form.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 10 '07 #3

P: n/a
On Dec 10, 12:16 am, Sasi <S...@discussions.microsoft.comwrote:
cool.
I ran your sample and noticed that if I omit "r" modifier i'll get the
result which is desired for me.
how can i do the same in J# ?
You'll need to look at the float formatting options of Java, which I
can't remember off-hand. However, just because it happens to be what
you want this time doesn't mean it always will be. You need to cope
with the fact that your result really *isn't* 20.1.

Jon
Dec 10 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.