Anu satya

Ranch Hand

Posts: 146

posted 6 years ago

Hi all,

I need to maintain the sign bit of the variable of type double.

My requirement is:

double total,num1,num2;

total = abs(num1)-abs(num2);

Examples:

1)if num1= -200 & num2=2, then total should be = -200

2)if num1 = 202 & num2 = -2, then total should be = 200

I don't know how to display the sign bit.

can any one please help?

Thanks

I need to maintain the sign bit of the variable of type double.

My requirement is:

double total,num1,num2;

total = abs(num1)-abs(num2);

Examples:

1)if num1= -200 & num2=2, then total should be = -200

2)if num1 = 202 & num2 = -2, then total should be = 200

I don't know how to display the sign bit.

can any one please help?

Thanks

With Regards,

Anu

Akhilesh Trivedi

Ranch Hand

Posts: 1608

posted 6 years ago

why are you using abs()?

And I dont get why

if num1= -200 & num2=2, then total should be = -200 ?

And I dont get why

if num1= -200 & num2=2, then total should be = -200 ?

Keep Smiling Always — My life is smoother when running silent. -paul

[FAQs] [Certification Guides] [The Linux Documentation Project]

Anu satya

Ranch Hand

Posts: 146

posted 6 years ago

sorry for the mistake.

if num1= -202 and num2 = 2, then total should be -200

I need to get the absolute value. So, I am using abs()

Please help

Akhilesh Trivedi wrote:why are you using abs()?

And I dont get why

if num1= -200 & num2=2, then total should be = -200 ?

sorry for the mistake.

if num1= -202 and num2 = 2, then total should be -200

I need to get the absolute value. So, I am using abs()

Please help

With Regards,

Anu

Akhilesh Trivedi

Ranch Hand

Posts: 1608

posted 6 years ago

And yes the program is doing what is supposed to do! !!

[FAQs] [Certification Guides] [The Linux Documentation Project]

Anu satya

Ranch Hand

Posts: 146

posted 6 years ago

Hi Jesper & Trivedi,

Thanks for the reply.

My requirement is:

At any point of time, the mathematical operation should be subtraction and if and only if num1 is -ve, then, sign of the total should be -ve.

Is there any better way to do this ? any suggession for performance turing?

Please help

Jesper de Jong wrote:I don't understand exactly what you're trying to do, but if you need to do different things depending on the sign of a variable, you can just check if the variable is less than zero or not with an if-statement.

Hi Jesper & Trivedi,

Thanks for the reply.

My requirement is:

At any point of time, the mathematical operation should be subtraction and if and only if num1 is -ve, then, sign of the total should be -ve.

Is there any better way to do this ? any suggession for performance turing?

Please help

With Regards,

Anu

posted 6 years ago

Ok, let's translate that into pseudo-code:

We have two numbers, a and b We subtract them: result = a - b We find out which one of a and b is the largest: max = Math.max(a, b) We check the sign of that number, and make sure to flip the sign of result if necessary: if ((max < 0 and result > 0) or (max > 0 and result < 0)) then result = -result;

Now you can translate this into Java code (if this is exactly what you want - I can see some points in the requirements that might be different from what you actually meant).

Anu satya wrote:Irrespective of sign on num1/num2, the mathematical operation should be subtraction and add sign of biggest number to the result.

Ok, let's translate that into pseudo-code:

Now you can translate this into Java code (if this is exactly what you want - I can see some points in the requirements that might be different from what you actually meant).

Anu satya

Ranch Hand

Posts: 146

posted 6 years ago

Hi Jesper,

I have given the code now. Please check my previous post.

please give suggessions for performance tuning.

Jesper de Jong wrote:Anu satya wrote:Irrespective of sign on num1/num2, the mathematical operation should be subtraction and add sign of biggest number to the result.

Ok, let's translate that into pseudo-code:

We have two numbers, a and b We subtract them: result = a - b We find out which one of a and b is the largest: max = Math.max(a, b) We check the sign of that number, and make sure to flip the sign of result if necessary: if ((max < 0 and result > 0) or (max > 0 and result < 0)) then result = -result;

Now you can translate this into Java code (if this is exactly what you want - I can see some points in the requirements that might be different from what you actually meant).

Hi Jesper,

I have given the code now. Please check my previous post.

please give suggessions for performance tuning.

With Regards,

Anu

posted 6 years ago

Having read this thread, and looked at your code, I agree with everyone -- I don't know why you are looking for the sign bit, when a simple comparison with zero will do.

But to answer your question.... with the IEEE 754 double precision format, the sign bit is the left most one. Just use the Double.doubleToRawLongBits() method to get the bits, and check the left one. This does seem much harder than just checking for less than zero, don't you think?

Henry

Anu satya wrote:

I have given the code now. Please check my previous post.

Having read this thread, and looked at your code, I agree with everyone -- I don't know why you are looking for the sign bit, when a simple comparison with zero will do.

But to answer your question.... with the IEEE 754 double precision format, the sign bit is the left most one. Just use the Double.doubleToRawLongBits() method to get the bits, and check the left one. This does seem much harder than just checking for less than zero, don't you think?

Henry

Campbell Ritchie

Marshal

Posts: 56570

172

posted 6 years ago

I presume you would use the & operator with 0x8000000000000000L and check whether the result is or is not 0, Henry, though I agree < 0 seems much more sensible. There are also methods called signum which you should seek in the API documentation.