473,382 Members | 1,749 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,382 software developers and data experts.

what's wrong with float ?

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
3 1680
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: puzzlecracker | last post by:
If you run this program, it will give very unexpected results. Can anyone explain the nature of this anamaly? (also what is the function call to execute 'pause'); #include<stdio.h> //void...
2
by: Mark Healey | last post by:
Here's the code: signed char xMove, yMove, subx, suby; xMove=(3*((float)rand()/(float)RAND_MAX))-1; yMove=(3*((float)rand()/(float)RAND_MAX))-1; I run srand() in main. When I run it...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
4
by: _Raven | last post by:
Okay, I am playing with submitting forms with Ajax. I am trying to adapt this script to my forms: http://www.captain.at/howto-ajax-form-post-get.php I have included my code at the bottom of this...
11
by: frankie_85 | last post by:
Hi everyone, I just made a simple code which is part of my assignment but I can't figure it out what's wrong with it. (always give me error messages) What the code basically does is: a...
30
by: Bill Reid | last post by:
#define MAX_VALUES 64 typedef struct { unsigned value_1; double value_2; double value_3; double value_4; } VALUES; typedef struct {
10
by: Jim Langston | last post by:
I use a game engine using MSVC++ .net 2003 and have no problems. Some users of DevC++ who use the same engine crash at times when a copy of this structure is the return variable. I don't have...
3
by: anollipian | last post by:
hi plz i have a problem i have a h.w to be done & my code have something wrong so plz try to help me the program wants the user to enter numbers until the user user enters a negative number then...
5
by: montybytes | last post by:
Hi there, Although, I have already placed this question in the HTML/CSS section, perhaps it might be worthwhile asking the question here as well. I have a JavaScript function which retrieves...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.