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

A problem with decimals...

Try this code, and tell me how can I fix it, please.

var num1=new Number(parseFloat("118.18"))
var num2=new Number(parseFloat("50"))
var num3=new Number(parseFloat("50"))
alert(num1-num2-num3)

This is very strange...
--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
Jul 20 '05 #1
5 5459
Cristian Martinello wrote:
Try this code, and tell me how can I fix it, please.

var num1=new Number(parseFloat("118.18"))
var num2=new Number(parseFloat("50"))
var num3=new Number(parseFloat("50"))
alert(num1-num2-num3)

This is very strange...

No its not. Javascript, like most programming languages, use floating
point arithmetic. Fractional numbers which are finite in one base may be
'repeating decimals' in another base. Floating point converts the
numbers to base 2, does the arithmetic, then converts the answer to base 10.

Rounding the answer to the precision of the operands is usually
sufficient. If not, convert the numbers to integers, perform the math,
then convert back.

Jul 20 '05 #2
"Cristian Martinello" <ca*********@tiscali.it> writes:
Try this code, and tell me how can I fix it, please.

var num1=new Number(parseFloat("118.18"))
var num2=new Number(parseFloat("50"))
var num3=new Number(parseFloat("50"))
alert(num1-num2-num3)
Just
alert(118.18-100)
will do the trick.
This is very strange...


This is completely normal when calculating with finite precission
binary-based floating point numbers.

The computer cannot represent 118.18 exactly. It finds a
representation that is close enough that when output, it is rounded to
118.18. Now you subtract 100 from it. That means that the integer part
of the number gets smaller, so there are more bits available for
the fractional part.

Imagine the number is stored as 15 decimal digits, but not exactly:
118.180000000001
When you output it, the least significant digit is rounded away.
Now subtract 100 and still use 15 decimal digits:
18.1800000000010
Suddently the error on the least significant bit is amplified, so
it won't go away by rounding.
The same problem happens with binary representation, typically when
making the number smaller.

<URL:http://jibbering.com/faq/#FAQ4_7>

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
"Cristian Martinello" <ca*********@tiscali.it> wrote in message
news:7d*************************************@mygat e.mailgate.org...
Try this code, and tell me how can I fix it, please.

var num1=new Number(parseFloat("118.18"))
var num2=new Number(parseFloat("50"))
var num3=new Number(parseFloat("50"))
alert(num1-num2-num3)

This is very strange...


It is not strange, it is normal. It is a manifestation of the inability
of an IEEE double precision floating point number (as used by JavaScript
for its one number type) to accurately represent all fractions. What you
are seeing is the nearest approximation of 18.18 that the number format
can accommodate. It is not a problem unique to computer floating point
number formats, how would you write 1/3 as a decimal fraction?
(precisely?)

<URL: http://www.jibbering.com/faq/#FAQ4_7 >

What is strange is wrapping a - new Number - call around a -
parseFloat - call. You would get the same results from:-

var num1="118.18"
var num2="50"
var num3="50"
alert(num1-num2-num3)

- as the subtraction operator will force the required string to number
type conversion.

Richard.
Jul 20 '05 #4
A typical trick to solve that is the following:

var result=Math.round((num1-num2-num3)*10000)/10000;
alert(result)

that is, you round something that you also m,ultiplicate by some power of
ten 8depending on how many floats you want to keep) and then you divide
exactly by that same amount of power of ten out of the rounding process -
that fixes it in a way that many use.

http://www.unitedscripters.com/

"Cristian Martinello" <ca*********@tiscali.it> ha scritto nel messaggio
news:7d*************************************@mygat e.mailgate.org...
Try this code, and tell me how can I fix it, please.

var num1=new Number(parseFloat("118.18"))
var num2=new Number(parseFloat("50"))
var num3=new Number(parseFloat("50"))
alert(num1-num2-num3)

This is very strange...
--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG

Jul 20 '05 #5
TrueBlue wrote:
A typical trick to solve that is the following:

var result=Math.round((num1-num2-num3)*10000)/10000;
alert(result)

that is, you round something that you also m,ultiplicate by some power of
ten 8depending on how many floats you want to keep) and then you divide
exactly by that same amount of power of ten out of the rounding process -
that fixes it in a way that many use.
Where's the point? Math.round(num1 - num2 - num3) will also do the trick.
[Fullquote below the text]


Please don't do this, bandwidth is precious.
PointedEars

Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Oswald | last post by:
Hey, how can I set the number of decimals? Example: 5 decimals 2.12345 2 decimals 2.12 I've found something about NumberFormat in the API, but without an example I can't solve this...
16
by: Ling Lee | last post by:
Hello. I'm trying to write a small program that lets you put in a number as an integer and then it tells you the textuel representation of the number. Like if your input is 42, it will say...
5
by: Peter Scheurer | last post by:
Hi, we found some strange behavior when operating with floats and round(). The following simplified statement reproduces the problem. select 6.56 - round(convert(float, 6.56), 2) from...
2
by: westjon64 | last post by:
i am fairly new to c++, i need to know how to make it so you can input decimals into a file using the cin >> a command instead of just being able to input whole numbers and also how to make it so...
6
by: Donal McWeeney | last post by:
Hi, Is there a way to specify on the predefined format strings like P and N that you want displayed all the decimals in the number... for example 3 will display 3 2.1 will display 2.1...
9
by: Gregory Petrosyan | last post by:
Hello, it's me again. I am trying to optimise small module for working with polynomials, and I have encountered problem: new, optimised, code, doesn't work in some specific case. Here's the old...
13
by: vsts2007 | last post by:
hi, how can I change the number of decimals appeared in access reports... I put a formula in query and its giving three to four decimals in the output of report. I want to change the number of...
4
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
7
by: olseni | last post by:
Hi all I hope someone can help me with this. I am sure there is a solution, but I cannot seem to find it.... I need to make a report with the following format: In the columns I have 5 samples,...
8
by: =?Utf-8?B?a2FyaW0=?= | last post by:
Hello All, why is this code is not showing the result in this format: 0.00 and showing it as only 0 Private Sub btn1_Click Debug.Print(Format$(Rnd() * 100, "0.00")) Dim d As Double =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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.