473,412 Members | 1,873 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,412 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 3158
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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...
0
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,...
0
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...

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.