473,216 Members | 2,013 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,216 software developers and data experts.

Help with Math.abs()

Anyone any idea why the Math.abs() isn't working in this code? I
thought it was supposed to convert negative numbers to whole numbers?

var se = [-27, -27, -27];

var time = 100

if(se[0] < 0){

Math.abs(se[0]); //<-- this isn't converting it to a whole number

se[0] += 1;

se[0] = (se[0]/(time / 4));

}

The result I get is -1.04 when it should be 1.12

May 21 '07 #1
5 3152
VK
On May 21, 4:58 pm, Yansky <thegoodd...@gmail.comwrote:
Math.abs(se[0]); //<-- this isn't converting it to a whole number
Not to a whole number, but to a positive number. Whole numbers have
nothing to do with Math.abs()

Math.abs() is a method that returns the value of its numeric argument
with sign set to positive. The argument itself doesn't change.
Math.abs(se[0]);
gets positive value of se[0] as requested - try
alert(Math.abs(se[0])); - but as you don't store this value anywhere
it gets lost. The actual value of se[0] is not affected by this
operation, in your case it remains negative. Either use intermediary
variable to store the value or for simple transformations:
se[0] = Math.abs(se[0])/(time / 4);

May 21 '07 #2
On May 21, 11:21 pm, VK <schools_r...@yahoo.comwrote:
On May 21, 4:58 pm, Yansky <thegoodd...@gmail.comwrote:
but as you don't store this value anywhere
it gets lost.
Ah of course. Thanks for the help. :)
May 21 '07 #3
Yansky wrote on 21 mei 2007 in comp.lang.javascript:
Anyone any idea why the Math.abs() isn't working in this code? I
thought it was supposed to convert negative numbers to whole numbers?
Math.abs(se[0]); //<-- this isn't converting it to a whole number
Next to what VK told you about abs,
this is not returning anything,
not even a positive number or zero.

Try:

result = Math.abs(se[0]);
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
May 21 '07 #4
VK
On May 21, 6:52 pm, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
Math.abs(se[0]); //<-- this isn't converting it to a whole number

Next to what VK told you about abs,
this is not returning anything,
not even a positive number or zero.
Why?

var se = [-27, -27, -27];

var result = Math.abs(se[0]);

window.alert(result); // alerts "27"
May 21 '07 #5
VK wrote on 21 mei 2007 in comp.lang.javascript:
On May 21, 6:52 pm, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
Math.abs(se[0]); //<-- this isn't converting it to a whole number

Next to what VK told you about abs,
this is not returning anything,
not even a positive number or zero.

Why?

var se = [-27, -27, -27];

var result = Math.abs(se[0]);
Because the

"var result = "

was not in the code of the OP.
window.alert(result); // alerts "27"


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
May 21 '07 #6

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

Similar topics

1
by: Brian Wilson | last post by:
Could someone help me with how to center or pocission this document to the center of the page? <body onload="ImageBook()"> <script type="text/javascript"> // 7 variables to control behavior...
11
by: Sambo | last post by:
I have the following module: ------------------------------- import math def ac_add_a_ph( amp1, ph1, amp2, ph2 ): amp3 = 0.0 ph3 = 0.0 ac1 = ( 0, 0j ) ac2 = ( 0, 0j )
1
by: JWest46088 | last post by:
I am having trouble figuring out how to add, subtract, multiply, and divide fractions in my Java program. First off I will tell you what I already did. At first, the assignment was to: Define a...
2
by: Chris Smith | last post by:
Howdy, I'm a college student and for one of we are writing programs to numerically compute the parameters of antenna arrays. I decided to use Python to code up my programs. Up to now I haven't...
1
by: mmc | last post by:
3 methods needs doing; wave1: This static method takes an AudioInputStream and gradually lowers and then raises the volume so that the new AudioInputStream appears to be constantly alerting in...
9
by: xxbabysue123xx | last post by:
THe question is: Modify the RationalNumber Class so that it implements the Comparable interface. To perform the comparison, compute an equivalent floating point value from the numerator and...
2
by: Head In A Pan | last post by:
Hello! My JavaScripting is at novice level - and after completing a tutorial on a 'floating layer' I was proud that I got it working on Firefox, Safari, Camino & even IE5 for mac!!! But not IE7!...
9
by: Ken Fine | last post by:
Hi there, I have written a simple function that attempts to set the angle of objects so as to place them in aesthetically appealing ways. The code follows; there is some stupidness in it (e.g. a...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.