0

Possible Duplicate:
tan 45 gives me 0.9999

When I use tan I get a long long answer:

double degrees = 45.0;
double radians = Math.toRadians(degrees);
System.out.println(Math.tan(radians));

When I run this code I get:

0.9999999999999 repeating

Why?

Community
  • 1
  • 1
  • Why do you expect an exact answer from conversion to radians followed by tan? The conversion to radians can be off by half a ulp (unit least place), and the Math.tan calculation can be off by up to one ulp. – Patricia Shanahan Feb 02 '13 at 13:56

2 Answers2

1

Because doubles (or in fact the system underling doubles, IEEE754) isn't perfect. Always be aware of this when working with doubles and floats (in any language, not just Java).

1

why not? this is near to one. floating point operations are never exactly. The reason is that numbers in the CPU are represented in binary form.
and a binary föoating point representation cannot exactly reprsent e.g 1/10.

Just read a bit about floating point programming.

AlexWien
  • 28,470
  • 6
  • 53
  • 83