0

I am trying to divide two full double values (i.e. double variables with all the memory filled up with a high precision number). Moreover, the result of the operation will be allocated in a double variable.

When I do the process, this one returns me NaN.

I read in some place before that two full double values when multiplied or divided between themselves will result in a high memory value, one that cannot be allocated in a double variable.

Am I right?

How could I solve this trouble?

public class CoeficienteAngular {

    public static double coefAngularImpar(    int x/*Coeficiente 
    da longarina a qual se analiza*/,         double y/*Distância 
    entre as longarinas*/,                     double z/*Somatorio*/){

        double beta;
        double betaPrim;

        betaPrim = (double) x/y;
        beta = betaPrim/z;

        return beta;
    }

    public static double coefAngularPar(    int x/*Coeficiente 
    da longarina que se analiza*/,             double y/*Distância 
    entre as longarinas*/,                     double z/*Somatório*/){

        double numerador, denominador, fatorCorrecao;
        double beta;

        int moduloL = Math.abs(x);

        numerador = (double)(moduloL-1)+(double)(1/2);
        denominador = y*z;

        fatorCorrecao = 1;//Inicializando a variável
        if(x>=0){
            fatorCorrecao = 1;
        }else if(x<0){
            fatorCorrecao = -1;
        }

        beta = fatorCorrecao*(numerador/denominador);
        return beta;
    }

}
Pokechu22
  • 4,984
  • 9
  • 37
  • 62
Sourisivre
  • 27
  • 5
  • What? What do you mean by _all memory filled up with a high precision number_? Please give an example. – Sotirios Delimanolis Nov 08 '14 at 03:12
  • I mean with all the decimals places occupied. For example: 0,333333333333333. I believe that is the most amount of decimals places possible to be allocated by a double variable. – Sourisivre Nov 08 '14 at 03:13
  • Again, give us an example. They can be occupied by 0s. – Sotirios Delimanolis Nov 08 '14 at 03:14
  • I have no idea what you are trying to say. Can you give an example with complete code? – markspace Nov 08 '14 at 03:16
  • [There are rules concerning floating-point division](http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.17.2), but unless you pinpoint where it is you're experiencing this, we couldn't tell you. I'd say, if you're getting overflow, you may want to switch to `BigDecimal` instead. – Makoto Nov 08 '14 at 03:19
  • I tried to use BigDecimal. But BigDecimal is a final type object. I thank you so much, but there are so many nested classes. It is not worth to copy and paste all here. – Sourisivre Nov 08 '14 at 03:25
  • Do you know that this `(double)(1/2)` will always be `0.0`? If not, then change it to `(1.0/2.0)` (without the cast). – Tom Nov 08 '14 at 03:28
  • Sincerely, I did not know. Thank you, Tom. I will correct it. But, if I do like you suggested won't "1/2" become "0"? – Sourisivre Nov 08 '14 at 03:37
  • 1
    That is why I wrote `1.0/2.0` :). This is the division of two floating point values which returns `0.5`. `(double)(1/2)` instead divides as integer, returns `0` and casts that result to double => `0.0`. You could also write `1D/2D`. The `D` (`d` would also work) marks the number as a `double` value. – Tom Nov 08 '14 at 03:40
  • kkkkk. I did like you suggested and now i got "infinite"... I am completely lost now. – Sourisivre Nov 08 '14 at 03:43
  • You will get `infinite` if the value of a `double` variable exceeds the limit for `double` types. More information about that: http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html. – Tom Nov 08 '14 at 03:47
  • Firstly, there's no such thing as a "full" or "empty" `double`. A double _always_ takes 64 bits, of which 52 are _always_ precision bits. Whether those bits are 0s or 1s doesn't make them more or less "full," any more than "0001" is a shorter string than "1111". Secondly, if the result of floating-point arithmetic is larger than the floating point can hold (in your case, because the two terms are very large), then the result "overflows" to infinity, which is a special value meant for just such situations. It sounds like you should read a tutorial, which is off-topic for SO. – yshavit Nov 08 '14 at 03:47
  • Thank you, @yshavit. In this case, how could I adjust the values in each variable to the result be able to be allocated in a double variable? Or never a multiplication between two double variables will be able to be allocated in a other double variable? – Sourisivre Nov 08 '14 at 03:52
  • If the result of the multiplication might be bigger than what a double can hold (which is an [absurdly large number](http://docs.oracle.com/javase/7/docs/api/constant-values.html#java.lang.Double.MAX_VALUE)), then yes, you'll need to do something else. Using `BigDecimal` is one option; another is to use a different unit, so that the same thing can be expressed in smaller numbers. For instance, if I could only count to 25,000, then I couldn't describe the circumference of the earth in feet (~131,480,000), but I could describe it in miles (24,901). – yshavit Nov 08 '14 at 04:14

1 Answers1

0

Don't use doubles. To avoid precision loss, use BigDecimal instead (see Javadoc). You can also read this post where people explained perfectly the reasons for using BigDecimal.

Community
  • 1
  • 1
Kraal
  • 2,779
  • 1
  • 19
  • 36