2007-08-13 14:06:31 +00:00
|
|
|
public class Floats {
|
|
|
|
private static void expect(boolean v) {
|
|
|
|
if (! v) throw new RuntimeException();
|
|
|
|
}
|
|
|
|
|
2007-10-15 19:12:38 +00:00
|
|
|
private static double multiply(double a, double b) {
|
|
|
|
return a * b;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static float multiply(float a, float b) {
|
|
|
|
return a * b;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static double divide(double a, double b) {
|
|
|
|
return a / b;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static double subtract(double a, double b) {
|
|
|
|
return a - b;
|
|
|
|
}
|
|
|
|
|
2007-08-13 14:06:31 +00:00
|
|
|
public static void main(String[] args) {
|
2007-10-15 19:12:38 +00:00
|
|
|
expect(multiply(0.5d, 0.5d) == 0.25d);
|
|
|
|
expect(multiply(0.5f, 0.5f) == 0.25f);
|
2007-08-13 14:06:31 +00:00
|
|
|
|
2009-03-05 00:52:06 +00:00
|
|
|
expect(multiply(0.5d, 0.1d) == 0.05d);
|
|
|
|
expect(multiply(0.5f, 0.1f) == 0.05f);
|
|
|
|
|
2007-10-15 19:12:38 +00:00
|
|
|
expect(multiply(0.5d, 0.5d) < 0.5d);
|
|
|
|
expect(multiply(0.5f, 0.5f) < 0.5f);
|
2007-08-13 14:06:31 +00:00
|
|
|
|
2009-03-05 00:52:06 +00:00
|
|
|
expect(multiply(0.5d, 0.1d) < 0.5d);
|
|
|
|
expect(multiply(0.5f, 0.1f) < 0.5f);
|
|
|
|
|
2007-10-15 19:12:38 +00:00
|
|
|
expect(multiply(0.5d, 0.5d) > 0.1d);
|
|
|
|
expect(multiply(0.5f, 0.5f) > 0.1f);
|
2007-08-13 14:06:31 +00:00
|
|
|
|
2009-03-05 00:52:06 +00:00
|
|
|
expect(multiply(0.5d, 0.1d) > 0.01d);
|
|
|
|
expect(multiply(0.5f, 0.1f) > 0.01f);
|
|
|
|
|
2007-10-15 19:12:38 +00:00
|
|
|
expect(divide(0.5d, 0.5d) == 1.0d);
|
2007-08-13 14:06:31 +00:00
|
|
|
|
2009-03-05 00:52:06 +00:00
|
|
|
expect(divide(0.5d, 0.1d) == 5.0d);
|
|
|
|
|
2007-10-15 19:12:38 +00:00
|
|
|
expect(subtract(0.5d, 0.5d) == 0.0d);
|
2008-05-23 00:08:41 +00:00
|
|
|
|
2009-03-05 00:52:06 +00:00
|
|
|
expect(subtract(0.5d, 0.1d) == 0.4d);
|
|
|
|
|
2008-05-23 00:08:41 +00:00
|
|
|
double d = 1d;
|
|
|
|
expect(((int) d) == 1);
|
|
|
|
|
2009-10-05 14:25:12 +00:00
|
|
|
float f = 1f;
|
|
|
|
expect(((int) f) == 1);
|
|
|
|
|
2008-05-23 00:08:41 +00:00
|
|
|
expect(Math.round(0.4f) == 0);
|
|
|
|
expect(Math.round(0.5f) == 1);
|
|
|
|
expect(Math.round(1.0f) == 1);
|
|
|
|
expect(Math.round(1.9f) == 2);
|
|
|
|
|
|
|
|
expect(Math.round(0.4d) == 0);
|
|
|
|
expect(Math.round(0.5d) == 1);
|
|
|
|
expect(Math.round(1.0d) == 1);
|
|
|
|
expect(Math.round(1.9d) == 2);
|
2007-08-13 14:06:31 +00:00
|
|
|
}
|
|
|
|
}
|