I am working on a calculator in Swift, but I have a small problem:
when multiplying two numbers, I don't have the same results as a regular calculator.
For example:
In Swift :
0.333328247070312 * 16 = 5.33325195312499
In a regular calculator:
0.333328247070312 * 16 = 5.333251953
What should I do to get the same results as a regular calculator in Swift?
Your "regular calculator" seems wrong or weird in result printing, so, you should not rely on it. I've rechecked your calculation in Python3 which is known to calculate all in binary64 double and print the most exact decimal form:
>>> 0.333328247070312 * 16
5.333251953124992
it's even more detailed (by one digit) than Swift output. Your output also can't be verified as binary32 calculation, because the latter has ~7 correct decimal digits, and usually isn't printed with more digits. What this calculator is? I'd suppose some Pascal-based tool due to its custom 6-byte float.
Try to ask your calculator to print the most detailed form. If it fails, throw it away and use a most exact tool to verify, or, if your task is really to get the same result, figure out more details about its processing.
Related
I am converting some Excel Calculations to Powershell:
=ROUNDDOWN((250000.00-0)*5.00%/12,6)
This gives a value of 1041.666666
I have converted it to Powershell:
[Math]::Round((250000.00 - 0) * (5.00/100) / 12,6)
But this gives a value of 1041.666667
The problem is that the last number matters and the fact that powershell doesnt round the 7 down to 6. This causes problems down the line as the final result for the Excel is 1461.59 but for powershell it is 1461.42
So, what's the best way to get a correct round-down to a certain number of decimal places in Powershell?
Math.Round rounds to the nearest number, not down. You'd have to roll your own version which uses Floor, e.g.:
function Get-RoundedDown($d, $digits) {
$scale = [Math]::Pow(10, $digits)
[Math]::Truncate($d * $scale) / $scale
}
Get-RoundedDown (250000 * .05 / 12) 6
Note, though, that in general there is no guarantee that the number you see as a result of that actually only has six digits after the decimal point since what we're doing here is not friendly towards binary floating-point numbers. So it may well be that you receive a number like 1441.666600000003 as a result and there's little you can do about that except switching to decimal.
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 have code and use double function several time to convert sym to double.to increase precision , I want to use digits function.
I want to know it is enough that I write digits in the top of code or I must write digits in above of every double function.
digits set's the precision until it is changed again. Calling digits() without any input you get the precision to verify it's set correct.
In many cases digis has absoluetly no influence on symbolic variables because an analytical solution is found. This means there are no precision errors unless you convert to double. When convertig, digits should be set to at least 16 because this matches double precision.
I need to generate a random number that is between .0000001 and 1, I have been using rand(1) but this only gives me 4 decimal points, is there any other way to do this generation?
Thanks!
From the Octave docs:
By default, Octave displays 5 significant digits in a human readable form (option ‘short’ paired with ‘loose’ format for matrices).
So it's probably an issue with the way you're printing the value rather than the value itself.
That same page shows the other output formats in addition to short, the one you may want to look in to is long, giving 15 significant digits.
And there is also the output_precision which can be set as per here:
old_val = output_precision (7)
disp (whatever)
old_val = output_precision (old_val)
Set the output_precision to 7 and it should be ok :)
Setting the output precision won't help though because the number can still be less than .0000001 in theory but you will only be displaying the first 7 digits. The simplest way is:
req=0;
while (req<.0000001)
req=rand(1);
end
It is possible that this could get you stuck in a loop but it will produce the right number. To display all the decimals you can also use the following command:
format long
This will show you 15 decimal places. To switch back go:
formay short
I'm having some difficulties processing some numbers. The results I get are some like:
0.000093145+1.6437e-011i
0.00009235+4.5068e-009i
I've already try to use format long and as alternative passing to string and then str2num and with no good results also. Although is not being possible to convert them properly as I want (e.g. to a number with 9 decimals) If nobody is able to help me, at least I would appreciate if someone can tell me how to interpret the meaning of the i base.
You are talking about the imaginary unit i. If you are just using real number, you could neglect the imaginary part (it is very small). Thus, try:
real(0.000093145+1.6437e-011i)
After taking real() you can also control the decimal place formatting by sprintf:
sprintf('%0.2f', pi)
Will result in:
'3.14'
Place a 9 instead of a 2 for 9 decimal places.