math problem

C

Chiwa

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
 
D

DJ WIce

: 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
 
B

Brian Genisio

Chiwa said:
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.25000002343589 inches instead? Or
1.2500000034322349872342229? (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
 
B

Brian Genisio

Cardman said:
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
 
J

Jim Ley

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.
 
D

Douglas Crockford

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.

JavaScript Numbers are 64-bit floating point as specified in IEEE 754.
It is the same representation that Java calls Double.

The scheme is intended to preserve as much precision as possible, but it
has some significant weakness. For example, it is not able to exactly
represent common fractions such as 1/10 or 1/100. It can at best
approximate them. As you can see, with a single operation you begin
accumulating noticeable error. Also, be aware that the associative law
and distributive law do not apply.

I think floating point is the wrong representation for numbers in most
applications. However, it is extremely popular, and popularity is much
more important these days than suitability or reliability. I recommend
that in applications that require exactness (for example, when working
with money) scale your values by a suitable factor (such as 100) so that
all of your arithmetic will be on whole numbers, which are exact.

http://www.crockford.com
 
D

DJ WIce

: 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.

I think this:
adding 0.0000001 and then use setPlaces(2);
will only be the solution when one want to have the code work in Netscape 4.
Exept that browser, a value of 4.1 will become 4.10 when directly used
setPlaces(2) (according to precious referred page).

Wouter






:
: Brian
:
:
:
 
D

DJ WIce

: I think this:
: adding 0.0000001 and then use setPlaces(2);
: will only be the solution ..
<snip>

Sorry correction:
The input is x.
Make it a string.
Then get the lastIndexOf(".") that string.

If it's -1 -> add ".000"
If it's string length-2 -> add "00"
If it's string length-1 -> add "000"
If it's string length -> add "000"

Now parceFloat and setPlaces(2).

Wouter
 
D

DJ WIce

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

Advice: remove the translation link for Dutch.
The page becomes none readable; it uses old words and a word by word
translation to the wrong words.

Welcome must be "Welkom"
Not welkomsgroet that can never be the first wordt in a line, so why it's
even chosen.. it's a very rare used word.

And that's only the first word. For all Dutch people the original page will
be way better and faster readable.

I think this holds for most Systran translations.
Even the tekst on their own page uses incorrect chosen words and bad
grammar.
(The Dutch flag link on http://www.systransoft.com/ results in:
http://w4.systranlinks.com/trans?systran_lp=en_nl&systran_url=http://www.systransoft.com/
)

"Perfectioneer voor e-mail"
should be:
geoptimaliseerd voor e-mail
or:
perfectioneer uw e-mails

And
".. vertaalsoftware voor de dagelijkse meertalige behoeften van kleine
ondernemingen."
should be "behoefte" instead of "behoeften" and so on.

Just to let you know.
Wouter
 
D

DJ WIce

<snip>
: So if you don't like the translation, then stick to the original
: English version.
What I was trying to tell you is that everyone in the Netherlands can read
and speak English at such level that your machone translation will never be
better readable than the English version. At 10 Dutch people will start
learning English and they must to that till they are at least 16.

Or let me tell give you an other argument;
One has to be very high educated to understand the translated page (very
high is above Univercity level in Dutch).

<snip>
: Yes... Still, for those people who do not know English..
There are no Dutch people who don't know English.
If a Dutch person can use internet he will be able to read Engish.
(remember that hacked software is mostly in English, so it's a must for
Dutch people to learn Engish - joke).

Wouter
 
D

DJ WIce

: >There are no Dutch people who don't know English.
:
: Did anyone ever tell you that English is spreading too far around the
: region? :-]
Most Dutch do also speak/understand German and have a 3 to 6 years education
in Frensh.
It's our custom to try to speak the language of the countries we visit for
holidays.

: subtitle instead of dub.
I dislike dubbing, some commercials are in dub, then I always zap to an
other channel (we "only" have 32).

: Does the words "Canal Digitaal" feature anywhere in the joke?
Only the analoge encoded channals are easy to decode without a
licence/decode formula.
For the digital channels we usually have a friend via the internet who knows
the codes for each month (because he works there) :-0

Wouter
 
B

Bas Cost Budde

What I was trying to tell you is that everyone in the Netherlands can read
Well, Wouter, not everyone. For Internet purposes though your statement
below is completely valid.
Then it is odd how my Atlas says that the languages used there are
Dutch and Frisian.

That is officially. Dutch and Frisian (the latter locally) can be used
by the government for communication.
In Holland, either Turkish or Maroc (er, what's that ending?) will be
the second language in frequency. I guess.
Still, it does not mention English for Sweden
either, when I know from my visit to Gothenburg that like everyone
there speaks English.

I think that they are trying to ignore the truth.

Agree; given only the 'dutch' version of the page, I think I still would
have failed to understand what it is about--let alone get the details.
In fact, to understand a bit of that version requires *quite a lot* more
English knowledge than the English original!

Know? My son doesn't know English. But then again, he's only 4. ~ ~ ~
Did anyone ever tell you that English is spreading too far around the
region? :-]

Have you ever tried to speak English with Asian people? You should!
I blame TV myself, with all that Hollywood programming, where they
even have the nerve to subtitle instead of dub. They should have done
like the Germans and banned the English language. ;-]

Yeah, and keep synchronized sound tracks. Yulck.

You call that a joke? I call it a chore! Software translation happens,
but most often it helps to know the original terms to understand what
the comments are about. Let alone the fact that English often takes up
less space on screen, so texts in Dutch get trunca someti.

I happen to have quite some customers where I'd better not use any
english word in my software (I happen to be a developer ;-) ) because
they don't get it then!
As a matter of fact, I think I can come up with quite a few people for
whom English is not a real option.

In the case of defending arguments (or on emotional subjects) I prefer
to use a neutral language. English suits me perfectly except when the
other party is a native speaker. You probably wouldn't want to take on
me in Dutch, would you?
 
D

DJ WIce

: >>There are no Dutch people who don't know English.
: Know? My son doesn't know English. But then again, he's only 4. ~ ~ ~
I hope he will know a bit when he's 8 :)
But I think he probable already knows some words ("Yes","No","Hello" or a
line or some from the telly).

: Have you ever tried to speak English with Asian people? You should!
LOL. Yeah, and on the same level:
I wander why the TU does ask them to come here to study, but then forgets to
teach them on what side of the road they shuld ride with their new bike.
On daily bases I see foreign studends expose their selfs in dangerous
situations, riding on the leftside of the road when a card rides on the
right side in oposite direction.. or even other bikes..

: As a matter of fact, I think I can come up with quite a few people for
: whom English is not a real option.
Hmm, well the "Discard" button makes manny people wander.
But that's mostly because WP5.1 was Dutch and so was their windows.
I think that sometimes people might be afrait for not understanding the
machine and then getting blackouts. I'm mostly visual geared so I even get
arround configurating some asian windows version.
I do also see that for older people (or other people who till now did not
work with computers) it's hard to distinguish differant windows programs.
They all look alike.
But if you put a child age of 4 behind a computer he mostly finds more
options and works with a mouse the way you would not expect within hours.

: In the case of defending arguments (or on emotional subjects) I prefer
: to use a neutral language. English suits me perfectly except when the
: other party is a native speaker.
Yeah, I think that's the way it should be. One can express himself best in
their native language.

: You probably wouldn't want to take on
: me in Dutch, would you?
Me? np :)

Wouter
 
W

wiesje janssen

Cardman said:
Then it is odd how my Atlas says that the languages used there are
Dutch and Frisian. Still, it does not mention English for Sweden
either, when I know from my visit to Gothenburg that like everyone
there speaks English.
<snip>
Sorry man, but the dutch translation is really horrible! No one will
be able to grasp what your site is about!
Wait, I'll re-translate the first line for you:

YOU: Welcome to my new web-site, which I hope you will like.
SYSTRAN: Welkomstgroet voor mijn opkomend zwemvlies- terrein, wie MIJ
hoop u zal zoals.
RETRANSLATED: Welcomegreetings for my nascending flipper- terrain, who
ME hope you will likewise.

Well, the manual that came with my Taiwanese microwave seems like the
'Kings English' compared to this. My advise: junk it and ask a
friendly dutchman to translate your page for you!

Groetjes, Wiesje
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top