math « double « Java Data Type Q&A

1. Why does Math.floor return a double?

Official javadoc says that Math.floor returns a double that is "equal to a mathematical integer", but then why shouldn't it return an int?

2. Is there any square root function more accurate than java.lang.Math.sqrt(double)

I am looking for square root functions (to use in java) that can give square roots upto atleast to 5-6 decimal places accurately. If there is a way to control accuracy ...

3. Javas Math.sin() produces NaN all the time

Forgive me if this is a dumb beginners problem, but I really don't get it. I have a member variable declared like so:

public Double Value;
When I assign 3.14159265 to Value and try ...

4. How to approximate double?

How to I write a function that approximates a double in the following manner, returning an int:

function (2.3) -> 2
function (2.7) -> 3
function (-1.2) -> -1
function (-1.7) -> -2

5. How do I convert a double to a fraction in a format suitable for representing inches?

I am using the Apache Commons math.Fraction class to convert my doubles to fractions, which is working fine. However, I need it to calculate these fractions in a way that ...

6. Java: questions about radians, Math.cos, Math.sin, double and long

I need to implement the harvesine distance in my java code. I found this snippet in Javascript, and I need to convert it to java.

  1. How can I convert latitude and longitude to ...

7. Getting Math.pow to return a correct value?

how can I make this expression not end up being a zero.

        double dBaseFitness = (double) baseFitness;

8. Java Doubles are not good at math

I am currently writing a calculator program in java. It is my first java program, I am used to c++. I have noticed that doubles in java are not at all like ...

9. Why is my math with Doubles coming out wrong?

This is how I am creating q

Double q = ((r * (i/5)) + y);
at this point the values of the other variables are
r = 3.470694142992069E-5
i = 1
y = -116.30237535361584
q = ...

10. Java equivalent to commons-math Stats that works on multi precision rather than double

I could translate the code to use BigDecimal or the Apfloat library rather than double but that would take considerable effort. I was hoping that there was an FOSS library that ...

11. Why is the last number wrong?

Why is only the last number wrong in the output this code:

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

12. Java double precision math

I'm doing a bit of modding to a popular game(Minecraft) and I seen these lines in the terrain generation

double d4 = 1.0D;
d4 *= d4; 
d4 *= d4;
d4 = 1.0D - d4;
double ...

13. Why does Math.ceil return a double?

This one threw me for a loop for a bit. When I call Math.ceil(5.2) the return is the double 6.0. My natural inclination was to think that Math.ceil(double a) would return ...

14. Java Math - Floor/Ceil returning double

Is there a good reason Math.floor() and Math.ceil() return double's instead of int (or more precisely long)? Is this just a matter of "it was done in JDK 1.0 this way and we can't change it" or is there some justification to it? I haven't seen a better explanation other than "it's what the API documentation says", which doesn't really answer ...

15. is Math.pow only used with type double?

I'm trying to calculate compound interest only using integers and I keep getting errors messages associated with this formula: amount = principal * Math.pow( 1.0 + rate, year ); I get stuff like: pow(double,double) in java.lang.Math cannot be applied to (int) or possible loss of precision found : double required: int amount = principal * Math.pow( 1.0 + rate, ...

16. double x = Math.sqrt(anynumber);

First off, many here don't take too well to folks who post the entire assignment, without showing any work as many will assume that the poster wants someone else to do the assignment for them. I will assume that this isn't you, that you don't want someone to do it all for you. Secondly, our track record for helping folks who ...