Dividing values gives me a small inaccuracy?

SirtyStan

So I've been building some unit conversion tools, and here's a snippet of code which contains the issue I have:

public static void main(String args[]) {
    double value = 100;
    System.out.println("Value in millimeters: " + value);
    value = ConversionTools.toFeet(ConversionTools.MILLIMETER, value);
    System.out.println("Value in feet: " + value);
    System.exit(0);
}
class ConversionTools {
// Start constants
public static final int MILLIMETER      = 0;
public static final int CENTIMETER      = 1;
public static final int DECIMETER       = 2;
public static final int METER           = 3;
public static final int DECAMETER       = 4;
public static final int HECTOMETER      = 5;
public static final int KILOMETER       = 6;
public static final int INCH            = 7;
public static final int FOOT            = 8;
public static final int YARD            = 9;
public static final int MILE            = 10;

private static final double KILOMETERS_TO_HECTOMETERS = 10;
private static final double HECTOMETERS_TO_DECAMETERS = 10;
private static final double DECAMETERS_TO_METERS = 10;
private static final double METERS_TO_DECIMETERS = 10;
private static final double DECIMETERS_TO_CENTIMETERS = 10;
private static final double CENTIMETERS_TO_MILLIMETERS = 10;

private static final double MILES_TO_YARDS = 1760;
private static final double YARDS_TO_FEET = 3;
private static final double FEET_TO_INCHES = 12;

private static final double METERS_TO_FEET = 3.28084;
// End constants

/**
 * Converts a distance to meters
 * @param type the type of distance
 * @param value the value of the distance
 * @return  returns the converted distance, or Double.MIN_VALUE
 * if the type is not recognized
 */
public static double toMeters(int type, double value) {
    if (type == MILLIMETER)         return toMeters(CENTIMETER, (double) value / CENTIMETERS_TO_MILLIMETERS);
    else if (type == CENTIMETER)    return toMeters(DECIMETER, (double) value / DECIMETERS_TO_CENTIMETERS);
    else if (type == DECIMETER)     return toMeters(METER, (double) value / METERS_TO_DECIMETERS);
    else if (type == METER)         return value;
    else if (type == DECAMETER)     return toMeters(METER, (double) value * DECAMETERS_TO_METERS);
    else if (type == HECTOMETER)    return toMeters(DECAMETER, (double) value * HECTOMETERS_TO_DECAMETERS);
    else if (type == KILOMETER)     return toMeters(HECTOMETER, (double) value * KILOMETERS_TO_HECTOMETERS);
    else if (type == INCH)          return toMeters(FOOT, toFeet(INCH, value));
    else if (type == FOOT)          return toMeters(METER, (double) value / METERS_TO_FEET);
    else if (type == YARD)          return toMeters(FOOT, toFeet(YARD, value));
    else if (type == MILE)          return toMeters(FOOT, toFeet(MILE, value));
    else                            return Double.MIN_VALUE;
}
/**
 * Converts a distance to feet
 * @param type the type of distance
 * @param value the value of the distance
 * @return returns the converted distance, or Double.MIN_VALUE
 * if the type is not recognized
 */
public static double toFeet(int type, double value) {
    if (type == INCH)               return toFeet(FOOT, (double) value / FEET_TO_INCHES);
    else if (type == FOOT)          return value;
    else if (type == YARD)          return toFeet(FOOT, (double) value * YARDS_TO_FEET);
    else if (type == MILE)          return toFeet(YARD, (double) value * MILES_TO_YARDS);
    else if (type == MILLIMETER)    return toFeet(METER, toMeters(MILLIMETER, value));
    else if (type == CENTIMETER)    return toFeet(METER, toMeters(CENTIMETER, value));
    else if (type == DECIMETER)     return toFeet(METER, toMeters(DECIMETER, value));
    else if (type == METER)         return toFeet(FOOT, (double) value * METERS_TO_FEET);
    else if (type == DECAMETER)     return toFeet(METER, toMeters(DECAMETER, value));
    else if (type == HECTOMETER)    return toFeet(METER, toMeters(HECTOMETER, value));
    else if (type == KILOMETER)     return toFeet(METER, toMeters(KILOMETER, value));
    else                            return Double.MIN_VALUE;
}

When I run this code, I end up getting this output:

Value in millimeters: 100.0
Value in feet: 0.32808400000000004

Does anyone know why I'm getting this extra 0.4E-16?

Also, for those who have an issue with the constants for the units of distance, I know I can use an enumerator for it but right now for simplicity's sake I'm using integers.

peter.petrov

Because Java implements an IEEE standard for floating point numbers. Operations with those numbers are usually not 100% precise.

Here is a very old Java article which should give you more details.

http://www.javaworld.com/jw-10-1996/jw-10-hood.html

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP dividing decimal/integer values gives round up output

From Dev

Small sample program gives me an error

From Dev

Dividing by small quantities in haskell

From Dev

SQL Server gives me wrong values?

From Dev

Dividing values within dataframe

From Dev

Dividing worksheet values

From Dev

Dividing OpenCV Mat gives unexpected result

From Dev

Dividing OpenCV Mat gives unexpected result

From Dev

Dividing all values of a dictionary by a value

From Dev

Dividing values in the row by the closest maximum

From Dev

Doubling and dividing floating point values

From Dev

JavaScript Dividing Array Values by an Int

From Dev

Dividing values in the row by the closest maximum

From Dev

Dividing slider values into range segments

From Dev

Doubling and dividing floating point values

From Dev

Dividing 2 input values in javascript

From Dev

Dividing values of dictionaries by values of another dictionary python

From Dev

Inaccuracy in decimals

From Dev

Inaccuracy in decimals

From Dev

Dividing by macro-defined int in C gives weird result

From Dev

Dividing two calculated values in mySQL select

From Dev

How to deal with null values while DIVIDING

From Dev

MySQL 'AND' gives me error

From Dev

It gives me an error at GPS

From Java

I try to give value for a 2 dimensional matrix using for loop in R, however it gives me unexpected NA values

From Dev

SQL Server Generate a specific set of year values from a column which gives me one single date/year

From Dev

Retrieving disc serial id gives me different values using FSO or ManagementObject

From Dev

SQL Server Generate a specific set of year values from a column which gives me one single date/year

From Dev

Floating point error when dividing one small range by another small range

Related Related

  1. 1

    PHP dividing decimal/integer values gives round up output

  2. 2

    Small sample program gives me an error

  3. 3

    Dividing by small quantities in haskell

  4. 4

    SQL Server gives me wrong values?

  5. 5

    Dividing values within dataframe

  6. 6

    Dividing worksheet values

  7. 7

    Dividing OpenCV Mat gives unexpected result

  8. 8

    Dividing OpenCV Mat gives unexpected result

  9. 9

    Dividing all values of a dictionary by a value

  10. 10

    Dividing values in the row by the closest maximum

  11. 11

    Doubling and dividing floating point values

  12. 12

    JavaScript Dividing Array Values by an Int

  13. 13

    Dividing values in the row by the closest maximum

  14. 14

    Dividing slider values into range segments

  15. 15

    Doubling and dividing floating point values

  16. 16

    Dividing 2 input values in javascript

  17. 17

    Dividing values of dictionaries by values of another dictionary python

  18. 18

    Inaccuracy in decimals

  19. 19

    Inaccuracy in decimals

  20. 20

    Dividing by macro-defined int in C gives weird result

  21. 21

    Dividing two calculated values in mySQL select

  22. 22

    How to deal with null values while DIVIDING

  23. 23

    MySQL 'AND' gives me error

  24. 24

    It gives me an error at GPS

  25. 25

    I try to give value for a 2 dimensional matrix using for loop in R, however it gives me unexpected NA values

  26. 26

    SQL Server Generate a specific set of year values from a column which gives me one single date/year

  27. 27

    Retrieving disc serial id gives me different values using FSO or ManagementObject

  28. 28

    SQL Server Generate a specific set of year values from a column which gives me one single date/year

  29. 29

    Floating point error when dividing one small range by another small range

HotTag

Archive