wfbarksdale
wfbarksdale

Reputation: 7606

Getting Math.pow to return a correct value?

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.

Upvotes: 0

Views: 770

Answers (2)

Steve Kuo
Steve Kuo

Reputation: 63094

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.

Upvotes: 1

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81694

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.

Upvotes: 1

Related Questions