Automatic scale shift
When I'm working with java.math.BigDecimal, and I want to divide, lets say, 3 over 567, it returns 0, or 0.0
I've found out that this problem can be solved by manually entering the desired scale of the result, but the program should know automatically what scale it ought to use. Does anyone know an algorithm that tells me what the scale (approx.) of the result will be?
R. Hollenstein
Hi Robin,
> When I'm working with java.math.BigDecimal, and I
> want to divide, lets say, 3 over 567, it returns 0,
> or 0.0
If you use BigDecimal.ROUND_DOWN that is.
> I've found out that this problem can be solved by
> manually entering the desired scale of the result,
> but the program should know automatically what scale
> it ought to use.
What do you mean by "know automatically what scale it ought to use" exactly?
Would the scale of a fraction say 3/4 (0.75) be 2 then?
But what about irrational numbers? Would their scale be the repetition of numbers after a division? Let's take your example: 3/567 = 0.005291005291005291... would that make a scale of 6 (005291)?
Regards,
Bart.
Well, in continued fractions, the scale would be defined as the point where the digits sequence begins to repeat itself. But yes, with the easy example 3 / 4, how can I define the scale as 2.By the way, I did use BigDecimal.ROUND_HALF_UPR. Hollenstein
> Well, in continued fractions, the scale would be
> defined as the point where the digits sequence begins
> to repeat itself.
So 1/3 = .3?
> But yes, with the easy example 3 /
> 4, how can I define the scale as 2.
There may be some function f(a,b) that gives the number of decimal places but I kind of doubt it or that it's simple because sometimes the answer is infinity. I would think that the most correct way to do this would be in the long division itself.
Is there a limit to the number of places that you can impose? Because you could just use that and then truncate the zeros and round the continuations. You can also use the relative orders of magnitude of the input to give a minimum scale.