473,725 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

math problem

Hey,

Expression:
Math.floor(x * 100) / 100

x= 4.1 gives 4.09, why in gods name?
While other values for x don't give a problem.

Thx in advance

Chiwa
Jul 20 '05 #1
40 2994
: Expression:
: Math.floor(x * 100) / 100
:
: x= 4.1 gives 4.09, why in gods name?

Because browsers are not calculators (here = sign is "can be").

4.1 = 4.100000000000
= 4.099999999999
= 4.100000000001

So:
4.1*100 = 410.0000000000
= 409.9999999999

So:
Math.floor(x * 100) = 410
= 409

And so your answer can be 409.
How more devisions, multiplications and roundings the bigger the margin to
the exact calculation may grow.

: While other values for x don't give a problem.
No, you just did not find them ;-)

Wouter
Jul 20 '05 #2
: Expression:
: Math.floor(x * 100) / 100

Try this:

x.setPlaces(2);

Wouter
Jul 20 '05 #3
: Try this:
:
: x.setPlaces(2);
See : http://www.mredkj.com/javascript/num...xample150.html

Wouter
Jul 20 '05 #4
DJ WIce wrote:
: Try this:
:
: x.setPlaces(2);
See : http://www.mredkj.com/javascript/num...xample150.html


http://www.jibbering.com/faq/#FAQ4_7

Is a better reference for the OP

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/

Jul 20 '05 #5
Chiwa wrote:
Hey,

Expression:
Math.floor(x * 100) / 100

x= 4.1 gives 4.09, why in gods name?
While other values for x don't give a problem.

Thx in advance

Chiwa

Just so you know, this is not just a limitation of Javascript. Many
other languages (C/C++ for instance) have this as an issue. That is
because Real numbers (floating point numbers) are _ALWAYS_
approximations. When you do a measurement of your finger (for
instance), and you measure exactly 1.25 inches... is that really
correct? Or could it be 1.2500000234358 9 inches instead? Or
1.2500000034322 349872342229? (You get the idea)

For this reason (and others dealing with the complexity of floating
point numbers), when a float (indescrete) is represented by binary data
(descrete), you will have some goofiness... it is guaranteed.

Because of this, most languages have methods for you to set the
precision (the number of significant figures), where rounding will occur
to the precision value you wish for.

If you desire an exact floating point value, such as in something like
money calculations, simply set your precisions.

You can also work in values multiplied by (10*order)... For instance,
$10.47 can be worked with as integers... immagine 1,047 pennies :) You
can convert it back by dividing or modding by (10*order) in integer form.

I have had to use that method once when developing assembly code on an
8-bit processor. There was no float support, so I needed to work in
values multiplied by (10*order), and then do some simple calculations to
provide useful user information.

Blah.... I wrote too much. Oh well, I hope it helps :)
Brian

Jul 20 '05 #6
"Chiwa" <Ch******@hotma il.com> writes:
Expression:
Math.floor(x * 100) / 100

x= 4.1 gives 4.09, why in gods name?
While other values for x don't give a problem.


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

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #7
On Sun, 01 Feb 2004 21:25:58 GMT, "Chiwa" <Ch******@hotma il.com>
wrote:
Hey,

Expression:
Math.floor(x * 100) / 100

x= 4.1 gives 4.09, why in gods name?
While other values for x don't give a problem.

Thx in advance


After working on my order form code it appears that I noticed and
solved this same problem long ago.

What I do is to add 0.000001 to the value x before putting it through
this same function, when this makes sure that the value is not below
the true value before you run the calculation.

Since this fraction is so small, then it would be rounded off
anywhere, where you only have to watch out that your x value does not
use this many decimal places.

That of course is not a problem for money values, like what I use it
for, with only two (or three with VAT) decimal places.

There you go, perfect rounding results every time.

Cardman
http://www.cardman.com
http://www.cardman.co.uk
Jul 20 '05 #8
Cardman wrote:
After working on my order form code it appears that I noticed and
solved this same problem long ago.

What I do is to add 0.000001 to the value x before putting it through
this same function, when this makes sure that the value is not below
the true value before you run the calculation.

Since this fraction is so small, then it would be rounded off
anywhere, where you only have to watch out that your x value does not
use this many decimal places.

That of course is not a problem for money values, like what I use it
for, with only two (or three with VAT) decimal places.

There you go, perfect rounding results every time.

Cardman
http://www.cardman.com
http://www.cardman.co.uk


You have got to be careful with a solution like this, since it is just a
hack, and can be confusing to maintainers in the future. You should
really use precision mechanisms built into the language to solve the
problem more elegantly.

Brian

Jul 20 '05 #9
On Mon, 02 Feb 2004 19:48:00 +0000, Cardman <do****@spam-me.com>
wrote:
That of course is not a problem for money values, like what I use it
for, with only two (or three with VAT) decimal places.

There you go, perfect rounding results every time.


however it is not a legal solution for calculating VAT involving Euros
say. Do it properly.

Jim.
--
comp.lang.javas cript FAQ - http://jibbering.com/faq/

Jul 20 '05 #10

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

Similar topics

1
1295
by: qwweeeit | last post by:
Another math problem easier to solve by hand, but, IMHO, difficult to program in a concise way like the solution of Bill Mill (linalg_brute.py) to the problem of engsol. I appreciated very much the Bill Mill's solution and that inspired in a certain way the solution for my problem. # Problem and solution: # ab * cb = dab 35 * 26 = 936
8
2311
by: McKirahan | last post by:
How do I add floating point numbers accurately? The following adds the 4 numbers 46.57, 45.00, 45.00, and 54.83 to give 191.39999999999998 instead of 191.40. <html> <head> <title>floats.htm</title> </head>
14
1907
by: marcokrechting | last post by:
Hi All, I have a rather complex math problem concerning dates. I will try to explain my problem. I have a table with the fields SUBJECT (text), DUE DATE (date) and CHECKED (yes/no). In this table I have some files made up of a name and date. For example: SUBJECT = "THIS.IS.MY.FILE", DUE DATE = "20050909", CHECKED = "false".
4
2656
by: nick | last post by:
I'm having a really weird problem. Take a look at this screenshot. http://users.aber.ac.uk/njb4/dotnetproblem1.jpg Notice the 4 watches at the bottom, _totalRecords, MyDataGrid.PageSize and TotalPages.Text. Now, you'd think that if _totalRecords was 11, and MyDataGrid.PageSize was 10... If you divide them, you get 1.1. If you Ceiling 1.1 them you'd get two.
5
1386
by: Todd | last post by:
OK...I have a form that has two text boxes. These two fields are being filled from a file import, which works fine. I am then passing these values into a custom class with both values typed as DOUBLE (IE: double.parse(mytextbox1.text) ). When I subtract these two values, I'm get an erroneous .00000000000002 being added to the result of the subtraction!! The form fills the instance of the class...
16
1770
by: Bill Nguyen | last post by:
I'm running into a very weird problem regarding subtraction. Subtraction behaves as if it's an addition in the below sub txtJacoCost.Text = Format(mRackc - (mDisc + mJaEc), "0.#####0") txtWfCost.Text = Format(mRackc - mDisc + mWfEC, "0.#####0")
5
1316
by: BEES INC | last post by:
I've been awfully busy programming lately. My Django-based side project is coming along well and I hope to have it ready for use in a few weeks. Please don't ask more about it, that's really all I can say for now. Anyways, I came across an interesting little math problem today and was hoping some skilled programmers out there could come up with a more elegant solution than mine. Problem: Star Ratings People can rate cheeseburgers on my...
0
1108
by: Gary Herron | last post by:
BEES INC wrote: Much simpler this way. This produces the number of whole start and the number of half stars: v = ... calculate the average ... whole = int(v+0.25) half = int(2*(v+0.25-whole))
2
2042
by: astrogirl77 | last post by:
Hi, I'm new to Python and am hoping to find help with coding a Python script, applet. I code in an old version of Visual Basic 4.0, I have a simple app that is about 3 and a half pages of code long it does some relatively simple math additions and subtractions The problem I have is that some numbers get to be very large integers and VB automatically converts this to scientifc notation, what I need is to have all numbers added and...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9179
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9116
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8099
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.