Java: Possible Loss of Precision When Using `Math.sin()`

flufflepuff

I am new to Java. I want to print out the sin, cos and tan values for every 15 degree interval.

public class W3Aufgabe5 {
    public static void main(String[] args) {

        System.out.printf("%6s%6s%6s%6s%n", "Angle", "Sin", "Cos", "Tan");
        System.out.println("------------------------");

        for (float angle = 0; angle <= 180; angle += 15) {

            float sin = Math.sin(Math.toRadians(angle));
            float cos = Math.cos(Math.toRadians(angle));
            float tan = Math.tan(Math.toRadians(angle));

            System.out.printf("%6d%6d%6d%6d%n", angle, sin, cos, tan);
        }
    }
}

For every Math.x() line, the compiler prints

error: possible loss of precision
required: float
found: double

I don't really understand. Why does it require double even though I am using float all the time?

rgettman

The trigonometric methods sin, cos, tan, and toRadians all accept double as a parameter and return double. There are no overloads that accept floats and return floats. It's legal to pass a float to a method expecting a double; it will be widened implicitly. But you can't narrow a double to a float implicitly when you assign the results to floats.

You can explicitly cast the results to float if you want, but you get better precision with double, so declare your sin, cos, and tan variables as doubles.

Also, the format specifier d means integer values, not floating point values. Use the format specifier f instead of d. You may also want to place some spacing in between your values when you print them.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java Math cubic root loss of precision

From Dev

Math in Java when precision is lost

From Dev

Possible loss of precision in java declaring bytes

From Dev

Initialize java byte array: possible loss of precision

From Dev

Loss of precision when using pow in C++

From Dev

Possible loss of precision error

From Dev

Loss of precision - Java

From Dev

Loss of precision - Java

From Dev

Loss of precision in java

From Dev

Avoiding Possible Precision Loss with a Simple Moving Average

From Dev

What is causing this "possible loss of precision" error?

From Dev

Possible loss of precision during conversion of float number

From Dev

Possible loss of precision error with an array in a loop

From Dev

Why is there a loss in precision when converting char * to float using sscanf_s or atof?

From Dev

Precision loss with java.lang.Double

From Dev

Loss of precision?

From Dev

Is it possible to cast with precision in java without using the formatted printf?

From Dev

negative infinity when using JAVA Math library

From Dev

negative infinity when using JAVA Math library

From Dev

Understanding "possible loss of precision required char found byte"

From Dev

Possible loss of precision between two different compiler configurations

From Dev

Possible loss of precision error vs. Type mismatch error

From Dev

Possible loss of precision between two different compiler configurations

From Dev

Floating point precision with Math.Ceil (Java)

From Dev

Precision loss / rounding difference when directly assigning double result to an int

From Dev

Loss of Precision when Converting Double to String in C++

From Dev

About a loss of precision when calculating an aggregate sum with data frames

From Dev

Using sin and cos math functions in bash

From Dev

Double comparison precision loss in C#, accuracy loss happening when adding subtracting doubles

Related Related

  1. 1

    Java Math cubic root loss of precision

  2. 2

    Math in Java when precision is lost

  3. 3

    Possible loss of precision in java declaring bytes

  4. 4

    Initialize java byte array: possible loss of precision

  5. 5

    Loss of precision when using pow in C++

  6. 6

    Possible loss of precision error

  7. 7

    Loss of precision - Java

  8. 8

    Loss of precision - Java

  9. 9

    Loss of precision in java

  10. 10

    Avoiding Possible Precision Loss with a Simple Moving Average

  11. 11

    What is causing this "possible loss of precision" error?

  12. 12

    Possible loss of precision during conversion of float number

  13. 13

    Possible loss of precision error with an array in a loop

  14. 14

    Why is there a loss in precision when converting char * to float using sscanf_s or atof?

  15. 15

    Precision loss with java.lang.Double

  16. 16

    Loss of precision?

  17. 17

    Is it possible to cast with precision in java without using the formatted printf?

  18. 18

    negative infinity when using JAVA Math library

  19. 19

    negative infinity when using JAVA Math library

  20. 20

    Understanding "possible loss of precision required char found byte"

  21. 21

    Possible loss of precision between two different compiler configurations

  22. 22

    Possible loss of precision error vs. Type mismatch error

  23. 23

    Possible loss of precision between two different compiler configurations

  24. 24

    Floating point precision with Math.Ceil (Java)

  25. 25

    Precision loss / rounding difference when directly assigning double result to an int

  26. 26

    Loss of Precision when Converting Double to String in C++

  27. 27

    About a loss of precision when calculating an aggregate sum with data frames

  28. 28

    Using sin and cos math functions in bash

  29. 29

    Double comparison precision loss in C#, accuracy loss happening when adding subtracting doubles

HotTag

Archive