# Removing negative value after truncating

• 08-04-2013, 05:13 PM
dojob
Removing negative value after truncating
The question is to print value for sin and cos for degree 0 to 360 increment by 10.
Below is the code i wrote but the problem is after the value sin 360 becomes -0.0000 instead of 0.0000 after limiting to 4 decimal places(which is required by the question). How to solve this issue?

Issit should be done by hardcoding if (value < -0.00000001, Math.abs(value)?

or should I used value * 10000 / 10000 to limit to 4 decimal places and use decimal format to display the trailing zero?

Code:

```    public static void main(String[] args) {         for(int degree = 0; degree <= 360; degree += 10){             System.out.print(degree + "\t\t");             System.out.printf("%.4f", Math.sin(degree * Math.PI / 180));         }     }```
• 08-04-2013, 05:22 PM
DarrylBurke
Re: Removing negative value after truncating
Quote:

Originally Posted by dojob
Issit should be done by hardcoding if (value < -0.00000001, Math.abs(value)?

Think carefully: what would that logic do?

db
• 08-04-2013, 05:33 PM
JosAH
Re: Removing negative value after truncating
You and I know that sin(x) == sin(k*360+x) but the poor computer doesn't know that; why not help it a bit?

Code:

```public static void main(String[] args) {         for(int degree = 0; degree <= 360; degree += 10){             System.out.print(degree + "\t\t");             int d= degree%360;             System.out.printf("%.4f", Math.sin(d * Math.PI / 180));         }     }```
kind regards,

Jos
• 08-04-2013, 05:47 PM
dojob
Re: Removing negative value after truncating
Quote:

Originally Posted by DarrylBurke
Think carefully: what would that logic do?

db

It will convert the insignificant negative value to absolute value 0.000000001 and after that 0.000 will be display? But it doesn't look proper to code it this way.
I have tried using decimal formatter and it seems the negative value still around for insignificant negative value.
• 08-04-2013, 05:49 PM
dojob
Re: Removing negative value after truncating
Quote:

Originally Posted by JosAH
You and I know that sin(x) == sin(k*360+x) but the poor computer doesn't know that; why not help it a bit?

Code:

```public static void main(String[] args) {         for(int degree = 0; degree <= 360; degree += 10){             System.out.print(degree + "\t\t");             int d= degree%360;             System.out.printf("%.4f", Math.sin(d * Math.PI / 180));         }     }```
kind regards,

Jos

Brilliant. A simple trick to implement the property of sin value where it is periodic over 360 degree. Thanks.
• 08-04-2013, 05:50 PM
JosAH
Re: Removing negative value after truncating
Quote:

Originally Posted by dojob
It will convert the insignificant negative value to absolute value 0.000000001 and after that 0.000 will be display? But it doesn't look proper to code it this way.
I have tried using decimal formatter and it seems the negative value still around for insignificant negative value.

Yep, the IEEE/754 format allows two different zeros: +0 and -0 and both compare equal to 0 but their String representation shows the difference.

kind regards,

Jos
• 08-04-2013, 05:57 PM
DarrylBurke
Re: Removing negative value after truncating
Yes, but the logic in #1 converts all values less than -0.00000001 to their absolute values.

db