Math.round question

B

bravesplace

Hello,

I am using the folling funtion to round a number to a single digit on
my form:

function round1(num)
{
return Math.round(num*1)/1
}


round1(150 / (X*1));

All is working well, but I need to make sure it only rounds down. How
can I accomplish this? Any assistance or code snips would be great.
 
V

VK

Hello,

I am using the folling funtion to round a number to a single digit on
my form:

function round1(num)
{
return Math.round(num*1)/1
}


round1(150 / (X*1));

All is working well, but I need to make sure it only rounds down. How
can I accomplish this? Any assistance or code snips would be great.

Math.floor(x);
 
R

RobG

Hello,

I am using the folling funtion to round a number to a single digit on
my form:

function round1(num)
{
return Math.round(num*1)/1

What is the point of multiplying by 1 then dividing by 1? The value of
'num' can be either a string or number primitive, it makes no difference.

According to the ECMAScript Specification section 15.8.2 (which lists
the function properties of the Math object, including round):

"Every function listed in this section applies the ToNumber
operator to each of its arguments (in left-to-right order
if there is more than one) and then performs a computation
on the resulting number value(s)."

}


round1(150 / (X*1));

All is working well, but I need to make sure it only rounds down. How
can I accomplish this? Any assistance or code snips would be great.

If simple truncation of the decimal part is required, then:

function toFloor(num){ return num|0; }


will do the job. It is claimed to be faster than Math.floor(), but may
be less maintainable as its effect is less obvious.
 
T

Thomas 'PointedEars' Lahn

RobG said:
If simple truncation of the decimal part is required, then:

function toFloor(num){ return num|0; }


will do the job. It is claimed to be faster than Math.floor(),
but may be less maintainable as its effect is less obvious.

I seriously doubt this. How can executing code that needs to be parsed
and compiled first ever be faster than executing precompiled code?


PointedEars
 
R

Randy Webb

Thomas 'PointedEars' Lahn said the following on 3/8/2006 7:09 PM:
I seriously doubt this. How can executing code that needs to be parsed
and compiled first ever be faster than executing precompiled code?

It's easy enough to test:

function toFloor(num){ return num|0; }
iterations = 1000000;
var begin1 = new Date();
for (i=0;i<iterations;i++){
var k = toFloor(3.45);
}
var end1 = new Date();
var totalTime1 = end1 - begin1;
document.write('toFloor() took ' + totalTime1 + ' milliseconds<br>')

var begin2 = new Date();
for (i=0;i<iterations;i++){
var k = Math.floor(3.45);
}
var end2 = new Date();
var totalTime2 = end2 - begin2;
document.write('Math.floor() took ' + totalTime2 + ' milliseconds<br>')
 
R

RobG

Randy said:
Thomas 'PointedEars' Lahn said the following on 3/8/2006 7:09 PM:

If a direct call to Math.floor is compared with the use of bitwise OR
wrapped in a function (as per Randy's test), then your doubts are
confirmed - but that's not what I was suggesting.

The OP posted a function called round1() that wrapped Math.floor. I was
suggesting that the entire function be replaced, not that round1()
should call toFloor().


Nor did I say 'It is claimed' for no reason:

"Math.ceil/floor Vs parseInt Vs plus/minus"
<URL:http://groups.google.co.uk/group/co...nt+Vs+plus/minus&rnum=1&#doc_56a3f10f32a8b480>


Richard is correct, bitwise OR seems to be faster than Math.floor at
least in IE and Firefox, see below.

It's easy enough to test:

function toFloor(num){ return num|0; }
iterations = 1000000;
var begin1 = new Date();
for (i=0;i<iterations;i++){
var k = toFloor(3.45);
}
var end1 = new Date();
var totalTime1 = end1 - begin1;
document.write('toFloor() took ' + totalTime1 + ' milliseconds<br>')

var begin2 = new Date();
for (i=0;i<iterations;i++){
var k = Math.floor(3.45);
}
var end2 = new Date();
var totalTime2 = end2 - begin2;
document.write('Math.floor() took ' + totalTime2 + ' milliseconds<br>')

The result of which is to show that a direct call to Math.floor is
faster than '|' wrapped in a function.

But the test is unfair - to make it even, either Math.floor() should be
wrapped in a function, as the OP had done, or '|' should be called
directly. If:

var k = toFloor(3.45);

is replaced with:

var k = 3.45 | 0;


then Math.floor and '|' are nearly identical for speed.

But the test is still flawed, the same number is being used
consistently. Replace 3.45 with Math.random()*5 - now '|' wins by a
small margin in Firefox and a very large margin in IE.

QED.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Wed, 8
Mar 2006 23:30:47 remote, seen in RobG
If simple truncation of the decimal part is required, then:

function toFloor(num){ return num|0; }


will do the job. It is claimed to be faster than Math.floor(), but may
be less maintainable as its effect is less obvious.

The usual claim is that num|0 is faster than Math.floor(num)
and gives the same answer for num<2^23.

If written as a function, there will be a function call/return overhead.

function toFloor(num){ return num|0; }

X = 123.45
j = 2e5
D0 = new Date()
k = j ; while (k--) {}
D1 = new Date()
k = j ; while (k--) Math.floor(X)
D2 = new Date()
k = j ; while (k--) (X|0)
D3 = new Date()
k = j ; while (k--) toFloor(X)
D4 = new Date()

j = [D1-D0, D2-D1, D3-D2, D4-D3]

// Raw Result 490,1980,930,3020
// Real Time 0,1490,440,2530
 

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
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top