开发者

Getting Math.pow to return a correct value?

开发者 https://www.devze.com 2023-02-17 13:41 出处:网络
how can I make this expression not end up being a zero. double dBaseFitness = (double) baseFitness; x.shortfitness = (long)(Math.pow(dbaseFitness, dbaseFitness/8开发者_开发技巧.0)/100.0)

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

        double dBaseFitness = (double) baseFitness;
        x.shortfitness = (long)(Math.pow(dbaseFitness, dbaseFitness/8开发者_开发技巧.0)/100.0)
                *(long)(Math.pow((double)x.genome.GSIZE, -.1)/100.0);

x.shortfitness is a long value. x.Genome.GSIZE is an int.

The only reason I am dividing each expression by 100 is so that the result doesn't exceed the max value of the long type.


Is baseFitness an integer? If so then baseFitness/8 does integer division. For example 7/8 will yield 0. Change your 8 and 100 to 8d and 100d to force floating point division.


If both the operands of an arithmetic expression are integral types, then the result will be too. So, for example, if "baseFitness" is an integer less than 8, then baseFitness/8 will be zero. Use "8.0" and "100.0" instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消