sign double

S

Sharp

Hi

I have a double number that is of a negative value.

e.g.,
double aValue = -0.1;

I would like to convert this double into a positive value.
What would be the easiest way?

Any help appreciated.

Cheers
Michael
 
G

Gordon Beaton

I have a double number that is of a negative value.

e.g.,
double aValue = -0.1;

I would like to convert this double into a positive value.
What would be the easiest way?

Subtract it from zero:

anotherValue = 0.0 - aValue;

or multiply it by -1:

anotherValue = -1.0 * aValue;

/gordon
 
R

Roland

Hi

I have a double number that is of a negative value.

e.g.,
double aValue = -0.1;

I would like to convert this double into a positive value.
What would be the easiest way?

Any help appreciated.

Cheers
Michael
If you know the value is negative, then simply negate it.
double aValue = -0.1;
double bValue = -aValue;
--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 
T

Thomas Schodt

Sharp said:
Hi

I have a double number that is of a negative value.

e.g.,
double aValue = -0.1;

I would like to convert this double into a positive value.
What would be the easiest way?

I was tempted to say

(aValue<0)?-aValue:aValue

but...

Well, it is how Math.abs() is implemented for scalars

public static int abs(int a) {
return (a < 0) ? -a : a;
}

public static long abs(long a) {
return (a < 0) ? -a : a;
}

However, for fp the implementation explicitly
compares (*) to zero and subtracts from zero.

(*) using "<=" instead of "<"

Apparently to handle "negative zero".

public static float abs(float a) {
return (a <= 0.0F) ? 0.0F - a : a;
}

public static double abs(double a) {
return (a <= 0.0D) ? 0.0D - a : a;
}

OFC, if you use Math.abs(), you don't have to worry about that.
 

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
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top