# Thread: Removing negative value after truncating

1. Member
Join Date
Jul 2013
Posts
52
Rep Power
0

## 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?

Java 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));
}
}```
Last edited by dojob; 08-04-2013 at 05:15 PM.

2. ## Re: Removing negative value after truncating

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

3. ## 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?

Java 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

4. Member
Join Date
Jul 2013
Posts
52
Rep Power
0

## Re: Removing negative value after truncating

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.

5. Member
Join Date
Jul 2013
Posts
52
Rep Power
0

## Re: Removing negative value after truncating

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?

Java 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.

6. ## Re: Removing negative value after truncating

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
Last edited by JosAH; 08-04-2013 at 05:51 PM. Reason: wrong IEEE number ..

7. ## Re: Removing negative value after truncating

Yes, but the logic in #1 converts all values less than -0.00000001 to their absolute values.

db

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•