Rounding fraction values?

T

Tuxedo

Tom Cole wrote:

[...]
I don't know if this is the perfect solution but have you tried using
something like value = ((value * 100) - 4) / 100 ?

With the small addition of Math.floor, the following returns the desired
and unfractioned list of values in this example:

value = 1.0

while (value >= 0.04){
value = (Math.floor(value * 100) - 4) / 100
document.write(value +'<br>')
}

writes:

0.96
0.92
0.88
0.84
0.8
0.76
0.72
0.68
0.64
0.6
0.56
0.52
0.48
0.44
0.4
0.36
0.32
0.28
0.24
0.2
0.16
0.12
0.08
0.04
0
 
D

Dr J R Stockton

In comp.lang.javascript message said:
Randy Webb wrote:

[..]
The illusion that you had an IQ above 10 would be a start.

I'm not sure what the bickering is all about!? I thought this was a
civilized NG ;-)

By sufficiently reading the newsgroup before posting, you could have
discovered in advance what a poster needs to do in order to appear
civilised.
 
V

VK

That would be a ludicrous way to build a system; I am surprised that
even you might suggest it.

The sensible way is to have a queue of events, ordered by the time when
it is desired that they should occur.
<snip>

I truly missed the need of this explanation unless "even you"
insertion was the only purpose ;-)
Both explanations (mine and yours) is a profanation of the real
mechanics anyway. Yours adds a bit of more details atop, but IMO it's
the case when "either say everything or limit by a basic hint". The
exact mechanics with IRQ, queues, dispatchers etc. cannot be described
in one or two paragraphs of plain text, so why to try?
My intention was to hint users that setTimeout(doSomething, 50) is
_not_ running in 50ms unless by some really strange coincidence.
On a bigger scale I tried to hint that any PC with OS for PS installed
is not a real-time system and that by switching from JavaScript to say
C++ or Java your PC doesn't become a strategic emulator on Cray
clusters :)
 
T

Tuxedo

Dr J R Stockton wrote:

[...]
By sufficiently reading the newsgroup before posting, you could have
discovered in advance what a poster needs to do in order to appear
civilised.

Sorry I don't follow you. Please feel free to elaborate for those who don't.
 
D

Dr J R Stockton

In comp.lang.javascript message said:
With the small addition of Math.floor, the following returns the desired
and unfractioned list of values in this example:

value = 1.0

while (value >= 0.04){
value = (Math.floor(value * 100) - 4) / 100
document.write(value +'<br>')
}

There you are assuming, which happens to be true in this case, that
value*100-4 will never be below the nominal integer value. If the 4
is changed to 3, there is an error at about 0.55; if to 7, at 0.25 &
0.55. The method is unsound. Understand IEEE-754 and FAQ 4.7.


value = 100
while (value >= 4) {
value = value - 4
document.write(value/100 +'<br>')
}

writes the right numbers reliably.


for (value = 96 ; value >= 0 ; value -= 4 )
document.write(value/100 +'<br>')

is probably how most experienced people would do it, except when speed
is crucial.


<FAQENTRY> The page linked to from FAQ 4.7 is neither well-written nor
accurate. The link should be supplemented by one to a well-written and
accurate page. Suggestions?

Seek via
<http://www.efg2.com/Lab/Library/Delphi/MathInfo/#FloatingPointNumbers>
<http://web.archive.org/web/20040406121124/http://www.mindspring.com/~ef
d/acc101.htm>
<http://docs.sun.com/source/806-3568/ncg_goldberg.html> (maybe not best
URL for it?)
Google "John Herbster" with Delphi & not Dental.
 
V

VK

By sufficiently reading the newsgroup before posting, you could have
Sorry I don't follow you. Please feel free to elaborate for those who don't.

IMHO the keyword here is "aristocratic netiquette".

"Now, an aristocrat, you know, the world over, has no human
sympathies, beyond a certain line in society. ... What would be
hardship and distress and injustice in his own class, is a cool matter
of course in another one."
Harriet Beecher Stowe

:)
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top