Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I need to have $\pi$ with an accuracy of $10$ digits. My first guess would be

N[Pi, 10] 

However, this rounds to the last digit instead of just chopping of any additional digits and hence the last digit is $4$ (and not $3$, as it actually is).

I could do

N[FromDigits[RealDigits[Pi, 10, 10]], 10]

but I am pretty sure that there are nicer solutions.

share|improve this question
1  
Maybe IntegerPart[Pi 10^9]/10.^9:) –  Kuba Dec 16 '13 at 21:24
9  
Floor[N[Pi, 11], 0.000000001]. Frankly, this looks like a non-problem. –  Sjoerd C. de Vries Dec 16 '13 at 21:27
2  
If you just want to print it..StringTake[ToString[N[Pi, 11]], 11] –  george2079 Dec 16 '13 at 21:34
4  
3.141592653 :-) –  David Skulsky Dec 16 '13 at 22:51
    
Thanks for those suggestions. Actually, Pi is just an example for a number. So what I want is to get the first n digits of a number a, without rounding. –  traindriver Dec 17 '13 at 10:57
add comment

3 Answers 3

up vote 6 down vote accepted

If you only need 10 digits of precision there's a ComputerArithmetic package that allows you to specify your rounding method under limited conditions.

Needs["ComputerArithmetic`"]
SetArithmetic[10, RoundingRule->Truncation]

This will set up our arithmetic system, but in order to use it, we'll need to use ComputerNumber[]

N[ComputerNumber[Pi],10]

yields an output of 3.141592653

N[ComputerNumber[EulerGamma],10]

yields an output of 0.5772156649. But keep in mind this is 10 total digits of precision so if we do this:

N[ComputerNumber[100*EulerGamma],10]

we'll get 57.72156649 and not 57.7215664901

If you require more flexible precision you'll want to do what Sjoerd C. de Vries recommended. I've wrapped everything in a SetPrecision[] for clarity because without it Mathematica will hide the output of everything but the first 6 significant digits:

NTrunc[number_, precision_Integer] := 
 SetPrecision[
  Floor[
   N[number, precision + 1], 
   10^-(precision - 1)], 
  precision]
share|improve this answer
add comment

Here is a round-about method, after the BBP algorithm (discovered by a computer! * ) :-

Round[NSum[
   (4/(8 n + 1) - 2/(8 n + 4) - 1/(8 n + 5) - 1/(8 n + 6))/16^n,
   {n, 0, 5}], 10.^-9] // InputForm

3.141592653

* "The formula itself was found by a computer program, and almost certainly constitutes the first instance of a computer program finding a significant new formula for pi." - D.H.Bailey

share|improve this answer
    
hmm.. i think this only works because the approximation happens to round the right way. Were there a base 10 anaolg of the bpp formula then you'd have something. –  george2079 Dec 16 '13 at 23:38
    
@george2079 - yes, the approximate rounding was convenient ;-) –  Chris Degnen Dec 16 '13 at 23:46
    
I hope it was obvious that I only summed to the 5th term rather than infinity, as the proper formula has it. –  Chris Degnen Dec 16 '13 at 23:54
    
interesting! but as I just commented above, I am not only interested in Pi but also other numbers. –  traindriver Dec 17 '13 at 11:00
    
@traindriver - then I would have said your RealDigits method was best, but hifigi's ComputerNumber is even better, and it works with negative numbers. –  Chris Degnen Dec 18 '13 at 9:11
add comment

I liked george2079 solution in the comments above, so here is a Manipulate using it, just for fun (I have slow coffee maker :)

enter image description here

Manipulate[
 n,
 Grid[{
   {N[Pi, 50], SpanFromLeft},
   {Dynamic[StringTake[ToString[N[Pi, n]], n]], SpanFromLeft},
   {Control[{{n, 10, "N?"}, 2, 50, 1, ImageSize -> Medium}], 
    Dynamic[n]}
   }, Alignment -> Left],
 TrackedSymbols :> {n},
 ImageMargins -> 0,
 FrameMargins -> 0,
 ContentSize -> {0},
 AppearanceElements -> "ManipulateMenu",
 Paneled -> False
 ]
share|improve this answer
    
nice one! but as stated above, Pi just served as an example for any number... –  traindriver Dec 17 '13 at 10:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.