What would be the most efficient way to grab the, say, 207th decimal place of a number? Would it just be x * Math.pow(10,207) % 10
?

- 305,947
- 44
- 307
- 483

- 1
- 2
-
Most efficient but not likely to be very accurate. – user207421 May 22 '20 at 02:37
-
Duplicate of https://stackoverflow.com/questions/26048153/getting-the-nth-decimal-of-a-float? – tanmay_garg May 22 '20 at 02:49
-
For all those who think this is impossible, please try 1E-207 to 9E-207 in the OP's formula and then explain the results. – user207421 May 22 '20 at 03:45
3 Answers
***Special Note: There is no: "This will handle all situations" answer here as the arbitrary value such as 207 could take the calculations way outside the bounds of possible precision of the variable types involved. My answer as such will only work within the bounds of variable type precision for which 207 is really not possible...
To get the specific digit an arbitrary number (like 207) of places after the decimal point... if you just multiply by factor of 10.. and then take mod 10, the answer (in java) is still a floating point type... not a single digit...
To get a specific digit an arbitrary number (n) of places after the decimal point, without converting to string:
Math.floor(x*Math.pow(10,n)) % 10;
to get 4th digit after 2.987654321
x*Math.pow(10, 4) = 29876.54321
Math.floor(29876.54321) = 29876
29876 % 10 = 6

- 1,416
- 2
- 9
- 13
-
Reason for downvote: Answer is misleading. The 207th digit of any given double or float is meaningless. `Math.pow(10, 207)` _IS_ representable (just barely) - `Math.power(10, 308)` is the largest power of 10 that doesn't just produce 'Infinity', but attempting to use this to 'extract the 207th' digit is meaningless. doubles don't have 207 digits worth of precision. Not even close. The number you'd get with this formula is arbitrary. `random.nextInt(10)` would be a lot more efficient if that's what you wanted. – rzwitserloot May 22 '20 at 03:17
-
@rzwitserloot Try `5.0E-207` in this formula, or the OP's, and then please explain the results. He is conflating prevision with range. You are talking about digits, but the the OP is talking about decimal places. – user207421 May 22 '20 at 03:40
-
understood and agree.. I don't like the down vote however as I only used the specific, seemingly arbitrary, values the user provided in the question... I will edit my answer to make clear that the conditions of any answer that anyone could provide are limited to reasonable expectations of precision based on the variable type – DynasticSponge May 22 '20 at 03:41
-
@DynasticSponge I do not agree, and neither does the experiment I proposed. The 207th decimal place of an FP number is not necessarily meaningless, or any decimal place up to 307 or whatever the maximum is. – user207421 May 22 '20 at 03:42
-
Guess I assumed the OP was looking for a single digit because of the % 10 in the original post... also.. I think I there was some post lag there as my "I agree" was for rzwitserloot... not agreeing with user207421 – DynasticSponge May 22 '20 at 03:49
-
I know who you're agreeing with, and you're wrong to do so. You were right the first time. Baffling how informational comments magically disappear. – user207421 May 24 '20 at 12:32
What you want is impossible.
The only things in java that work with Math.pow
and basic operators are the primitives. The only floating point primitives are float
and double
. These are IEEE754 floating point numbers; doubles are 64 bits and floats are 32 bits.
A simple principle applies: If you have 64 bits, then you can only represent 2^64 different numbers (it's actually a little less). So, you get about 18446744073709551616 numbers, of all numbers in existence, which actually exist as far as the computer is concerned for doubles. all other numbers do not exist.
So what happens if a mathematical operation (say, 0.1 + 0.2
) ends up being a number that doesn't exist? Well, java (this is predicated by the IEEE754 standard; most languages and chips do it this way) will return you the nearest number amongst all the 18446744073709551616 numbers that do exist.
The problem with wanting the 207th digit is that obviously, given that only 18446744073709551616 numbers exist, none of those 18446744073709551616 numbers have that kind of precision. Asking for the 207th digit is therefore completely random. It says nothing about the input number... whatsoever.
Let me repeat that: There are no double values that have a significant 207th digit AT ALL.
If you want 'perfect' representation, with no rounding whatsoever, you want BigDecimal
, but note that demanding perfection is tricky. Imagine in basic decimal math (computers are binary, but lets stick with decimal as we're all much more familiar with it, what with our 10 fingers and all), I ask you to only give me perfect answers, and then I ask you to divide 1 by 3.
BigDecimal won't let you do that either, so the ops you can run on BigDecimals without telling BigDecimal in what ways it is allowed to be inprecise leads to exceptions.
If you've set it all up exactly how you wanted it, and you really have a BigDecimal with a 207th digit after the comma, you can use the scale feature, or just the power-of-10 feature to get what you want.
Note BigDecimal is not primitive and therefore does not support the +, %, etc operators.

- 85,357
- 5
- 51
- 72
-
Incorrect. You are confusing precision with range. You can recover 1.0E-207 to 9.0E-207 correctly with the OP's formula. – user207421 May 22 '20 at 03:39