math 5/2 returns 2.
I want 2.5 -- how do I get decimal values?
You can "force" math to return fractions by default -- use the -l option to bc:
$ math 5/2
2
$ function bc
command bc -l $argv
end
$ math 5/2
2.50000000000000000000
Add this to one of your config.fish files
set -x BC_ENV_ARGS ~/.bc.cfg
Then create a .bc.cfg file to tell it out many decimal places to display
echo "scale = 5" >> .bc.cfg
This will:
math 5/2
2.50000
you have to force floating point division instead of integer division
math 5/2.0
Related
I simply want to know what format I need to use to display irrational numbers to decimal places greater than 16 as per the Unicode standard.
I’ve tried things like:
set var [expr [format %.1000f 1] / [format %.1000f 9]]
And still only get 1/9 to 16 decimal places.
I need to increment 1/x for x < 1000 and see all the decimal places (up to at a minimum 1000).
Any tips ?
Tcl's native floating point math uses IEEE double precision floats; those only have around 15 or so meaningful significant figures (there's effectively 53 bits in the mantissa). Instead, you need to use an arbitrary precision floating point arithmetic package. Tcllib has one, math::bigfloat:
package require math::bigfloat
namespace import math::bigfloat::*
# How many digits do we want
set places 1000
set x [fromstr 1.0 $places]
set y [fromstr 9.0 $places]
set z [div $x $y]
puts [tostr $z]
You can also use math::exact from Tcllib, which does things a little differently internally (you specify the precision you want in the output at the end):
package require math::exact
namespace import math::exact::exactexpr
# This package uses *binary* precision, not decimal
set precision [expr {ceil(log(10)/log(2) * 1000)}]
set x [[exactexpr 1] ref]
set y [[exactexpr 9] ref]
set z [[exactexpr {$x / $y}] ref]
$z asFloat $precision
The ref stuff is to do with reference count management because expression objects are internally non-trivial.
I have to import a large integer (1000 digits) into matlab to run some calculations on it. However, when I import it I seem to loose accuracy due to the fact that matlab uses the scientific notation.
Is there any way that I can get the actual integer?
Here's the actual data I have to import:
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
Such a large integer cannot be represented in IEEE floating point standard. Check out this answer for the largest double that can be represented without losing precision (its 1.7977e+308). That can be obtained by typing realmax in MATLAB.
You can use vpi (available here, as mentioned in comment) or you can use the MATLAB in-built vpa.
This is how you use vpa
R=vpa('7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450');
You can check the following:
vpa('R+1000-R')
The answer of the above is 1000 as expected. Do not forget to put your expression in quotes. Otherwise, you are passing inifinity to vpa instead of the 1000 digit number.
If you want to use vpi, its a beautiful toolbox, go ahead, download it. Go into its root directory and run the following command:
a=vpi('7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450')
Well, the advantage with vpi is as follows:
The output of vpi:
a=vpi(<<Your 1000 digit number in quotes>>); %output prints 1000 digits on screen.
The output of vpa:
R=vpa(<<Your 1000 digit number in quotes>>);
this prints:
R =
7.3167176531330624919225119674427e999
Also, with vpi, you can do something like this:
a=vpi('7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450')
b=a+1
b-a %output of this yields 1.
I somehow cannot do the operation of b-a in vpa and obtain the answer 1.
I am using Matlab and using numbers in scientific notation, which are represented with the letter e for the exponent. An example in Matlab:
>> 2e5
ans =
200000
Now would like to work with numbers in scientific notation, but using variables to hold the values of the mantissa and the exponent (left and right side of the e respectively). I do not understand how this can be done without the variable names merging with the letter e for the exponent. Eg:
>> rr=5;
>> 2err
??? 2err
|
Error: Unexpected MATLAB operator.
Can this still be done? Or must I use the manual approach:
>> 2*10^rr
ans =
200000
You must use the manual approach; you can't use scientific notation like that with variables. You might want to use 2.*10.^rr, with the ., to enable you to use the same statement with arrays of numbers.
I have several equations mixed throughout a document, appearing in the following forms:
5^4 %A
3^-1 %B
5.01 x 10^2.05 %C
5.01 x 10^2 %D
-5 x 10^3 %E
In other words, they fit in the format of x^y, or z * x^y, where z, x, and y can be any integer or rational number (expressed with a decimal point), positive or negative.
I wish to convert these to math mode for TeX. E.g.:
$5.01 \cdot 10^2$
With much assistance from others, I have managed to create this BASH script with sed to solve items A and B:
sed "s/\-\{0,1\}[0-9]\{1,\}^\-\{0,1\}[0-9]\{1,\}/$&$/" input > output
This is able to convert items A and B to math mode, but I found it only converts the first occurrence it finds within a line. For instance, if a line says 5^10 is greater than 1^2 it converts this to $5^10$ is greater than 1^2. A second pass with the script results in $$5^10$$ is greater than 1^2.
I managed to modify the above script to handle items C, D, and E, but cannot figure out how to handle the back second part (I have marked it with "???"):
sed "s/\-\{0,1\}[0-9]\{1,\}\ x\ \-\{0,1\}[0-9]\{1,\}^\-\{0,1\}[0-9]\{1,\}/???/" input > output
This presents a problem:
Even if the above could work, if I first run the first sed script, then run the second, the first confuses the second, i.e. I would end up with 5.01 x $10^2.05$. If I ran the second script first, I would end up with $5.01 x $10^2.05$$ after running the second script.
In short, how can I perform this kind of conversion for all items within a document?
5^4 --> $5^4$
3^-1 --> $3^-1$
5.01 x 10^2.05 --> $5.01 \cdot 10^2.05$
5.01 x 10^2 --> $5.01 \cdot 10^2$
-5 x 10^3 --> $-5 \cdot 10^3$
but I found it only converts the first occurrence it finds within a line
Use the /g global replacement flag.
Converting your text is best done in several passes
Pass 1
sed 's/\(-\?[0-9].\?[0-9]*\) x \(-\?[0-9]\{1,\}\)^\([0-9]\{1,\}\.\?[0-9]*\)/$\1 cdot \2^^\3$/g' input > tmp
What we've done here is capture \(...\) x \(...\)^\(...\) into the sed remembered patterns \1 \2 and \3 which we then use to convert the text.
This deals with your %C,%D,%E and for example converts 5.01 x 10^2.05 into $5.01 cdot 10^^2.05$. Note that we have converted the occurrences of ^ into ^^ temporarily.
Pass 2
sed -i 's/-\?[0-9]\+\^-\?[0-9]\+/$&$/g' tmp
This deals with your examples %A and %B. As we previously converted the ^ in 10^2.05 to ^^ this was ignored by pass 2 solving the problems you noted.
Pass 3
sed -i 's/\^^/^/g' tmp
Which simply converts the ^^ back into ^
Based on the output you need, will this following method work for you?
[jaypal~/Temp]$ cat file0
5^4
3^-1
5.01 x 10^2.05
5.01 x 10^2
-5 x 10^3
[jaypal~/Temp]$ sed -e 's/^/\$/' -e 's/$/\$/' -e 's/x/\\cdot/' file0
$5^4$
$3^-1$
$5.01 \cdot 10^2.05$
$5.01 \cdot 10^2$
$-5 \cdot 10^3$
This might work for you:
sed -i 's/\(-\?[0-9]\+\(\.[0-9]\+\)\? \)x\( -\?[0-9]\+\^-\?[0-9]\+\(\.[0-9]\+\)\?\)\|\(-\?[0-9]\+\^-\?[0-9]\+\)/$\1\\cdot\3\5$/g;s/\$\\cdot/$/g' file
although the GNU sed -r switch makes it look a lot less cluttered:
sed -ri 's/(-?[0-9]+(\.[0-9]+)? )x( -?[0-9]+\^-?[0-9]+(\.[0-9]+)?)|(-?[0-9]+\^-?[0-9]+)/$\1\\cdot\3\5$/g;s/\$\\cdot/$/g' file
In octave the output of float point numbers is limited by default to 4 digits (%.4f). Is there a way to set this behavior using IPython?
If you are using numpy a lot, then numpy.set_printoptions lets you tweak that.
Another option is to use the magic %precision command (only in IPython 0.11 or greater).
I guess that you can put your defaults in your startup configuration file.
%precision also takes a format argument, eg like this (gives you 2 digit precision and non-scientific format):
%precision %.2f
If you want to set the precision to 6 digits, try this:
%precision 6