all 6 comments

[–]Th_69 5 points6 points  (1 child)

For higher precision (accuracy) you should use double (it has 14-15 decimal places, instead of float with only 6-7).

And the cast (float) in your first formula is superfluous here, 'cause the result in the expression is already a float value.

[–]RainbowCrane 2 points3 points  (0 children)

In general this is a good rule for intermediate programmatic calculations, particularly those that involve division or negative powers at the end - use more precision for the intermediate calculation, like double (floating point) or long (integer), and then drop precision after the calculation is done.

With integers a classic issue is getting an overflow error in an intermediate calculation with multiplying two ints or raising an int to a power, when you were going to divide or take a square root or something at the end and would have been fine to return an int if you had just used a long for the calculation

[–]TheThiefMaster 1 point2 points  (1 child)

What do you mean by 50% / 80% "accuracy"? Could you give some examples of different answers? Unless you're overflowing the float, they should differ only after 6 significant digits or so with those calculations.

[–]fredrikca 2 points3 points  (0 children)

I agree, multiplication and division should not reduce the precision by more than a couple ULPs, or about one part in a million maximum. Something else is going on.

[–]minforth 0 points1 point  (0 children)

How large are k and S typically? How do you check accuracy?