From 04583ea534a30048978891121cb2df8a442c507d Mon Sep 17 00:00:00 2001 From: Josh warner Date: Thu, 6 Aug 2009 10:34:28 -0600 Subject: [PATCH] floating point test code --- test/AllFloats.java | 77 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 test/AllFloats.java diff --git a/test/AllFloats.java b/test/AllFloats.java new file mode 100644 index 0000000000..8f41c4d20e --- /dev/null +++ b/test/AllFloats.java @@ -0,0 +1,77 @@ +public class AllFloats { + private static float multiplyByFive(float a) {return 5f * a;} + private static double multiplyByFive(double a) {return 5d * a;} + private static float multiply(float a, float b) {return a * b;} + private static double multiply(double a, double b) {return a * b;} + private static double multiply(float a, double b) {return a * b;} + private static float divide(float a, float b) {return a / b;} + private static double divide(double a, double b) {return a / b;} + private static double divide(float a, double b) {return a / b;} + private static float add(float a, float b) {return a + b;} + private static double add(double a, double b) {return a + b;} + private static double add(float a, double b) {return a + b;} + private static float subtract(float a, float b) {return a - b;} + private static double subtract(double a, double b) {return a - b;} + private static double subtract(float a, double b) {return a - b;} + private static float complex(float a, float b) {return (a - b) / (a * b) + (float)Math.sqrt(a);} + private static double complex(double a, double b) {return (a - b) / (a * b) + Math.sqrt(a);} + private static double complex(float a, double b) {return (a - b) / (a * b) + Math.sqrt(a);} + private static int f2i(float a) {return (int)a;} + private static long f2l(float a) {return (long)a;} + private static float i2f(int a) {return (float)a;} + private static double i2d(int a) {return (double)a;} + private static int d2i(double a) {return (int)a;} + private static long d2l(double a) {return (long)a;} + private static float l2f(long a) {return (float)a;} + private static double l2d(long a) {return (double)a;} + private static float negate(float a) {return -a;} + private static double negate(double a) {return -a;} + private static int abs(int a) {return Math.abs(a);} + private static float abs(float a) {return Math.abs(a);} + + private static void expect(boolean v) { + if(!v)throw new RuntimeException(); + } + + private static int last(){return 0;} + + public static void main(String[] args) { + expect(multiplyByFive(36f) == 5f * 36f); + expect(multiplyByFive(36d) == 5d * 36d); + expect(multiply(5f, 4f) == 5f*4f); + expect(multiply(5d, 4d) == 5d*4d); + expect(multiply(5f, 4d) == 5f*4d); + expect(divide(5f, 2f) == 5f/2f); + expect(divide(5d, 2d) == 5d/2d); + expect(divide(5f, 2d) == 5f/2d); + expect(add(5f, 4f) == 5f+4f); + expect(add(5d, 4d) == 5f+4d); + expect(add(5f, 4f) == 5f+4d); + expect(subtract(5f, 4f) == 5f-4f); + expect(subtract(5d, 4d) == 5f-4d); + expect(subtract(5f, 4f) == 5f-4d); + expect(complex(4f, 3f) == (4f-3f)/(4f*3f) + 2f); + expect(complex(4d, 3d) == (4d-3d)/(4d*3d) + 2d); + expect(complex(4f, 3d) == (4f-3d)/(4f*3d) + 2f); + + expect(f2i(4f) == 4); + expect(f2l(4f) == 4); + expect(i2f(4) == 4f); + expect(i2d(4) == 4d); + + expect(d2i(4d) == 4); + expect(d2l(4d) == 4); + expect(l2f(4) == 4f); + expect(l2d(4) == 4d); + + expect(negate(4f) == -4f); + expect(negate(4d) == -4d); + + expect(abs(-4) == 4); + expect(abs(12) == 12); + expect(abs(-4f) == 4f); + expect(abs(12f) == 12f); + + int unused = last(); + } +}