question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Missing precision

See original GitHub issue

I have trouble calculating a relatively simple expression “log(8/(2pi5cos(7/5)^2))(2*5)^(-8)” to the desired precision.

My code

import mpmath as mpm
from mpmath import mpf
mpm.dps = 100; # Target (decimal) precision

# Trying to calculate 
mpmmp = mpm.log(8/(2*mpm.pi*5*mpm.cos(7/5)**2))*(2*5)**(-8)
# also tried, but gives the same result:
# mpmmp = mpm.log(mpf(8)/(mpf(2)*mpm.pi*mpf(5)*mpm.cos(mpf(7)/mpf(5))**mpf(2)))*(mpf(2)*mpf(5))**(-mpf(8))
print(N(mpmmp, 50))

# Same thing in Wolfram Alpha https://www.wolframalpha.com/input?i=+log%288%2F%282*pi*5*cos%287%2F5%29%5E2%29%29*%282*5%29%5E%28-8%29
wamp = mpm.mpf("2.1764268381757881245184989320757924235425037995295963369536875540976739004791583229859268345586450088673471866819775813945E-8")
print(N(wamp, 50))

print(N(mpmmp-wamp, 50))

With the result

0.000000021764268381757866721875713136684360460293419237132
0.000000021764268381757879956765513985127158402832492356538
-1.3234889800848442797942539073119405657052993774414e-23

Why do they not match?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
To3No7commented, Aug 9, 2022

Ah, the “mpm.mp.dps = 100” solved it!

So many hours of frustration this has solved! Many Thanks!

1reaction
WarrenWeckessercommented, Aug 9, 2022

As I wrote in a comment I have already tried:

Sorry! I guess I skimmed the rest after seeing the mistakes in the code that is not commented out.

The problem is this line:

mpm.dps = 100; # Target (decimal) precision

It should be

mpm.mp.dps = 100  # Target (decimal) precision
Read more comments on GitHub >

github_iconTop Results From Across the Web

Format specifier missing precision error with string formatting
There is a small mistake in your format string: 'Converted: {:.lf}'. should be 'Converted: {:.1f}'. The only difference is that you have ...
Read more >
ValueError: Format specifier missing precision Python
ValueError: Format specifier missing precision Python ... To print the floating point numbers, you have to have atleast one of the inputs as...
Read more >
How to control decimal precision? : r/learnpython - Reddit
I get an error that states 'ValueError: Format specifier missing precision' # Find PI to the Nth Digit import math num = float(input("Enter ......
Read more >
Losing My Precision: Tips For Handling Tricky Floating Point ...
Loss of precision in converting into floating point; Adding numbers of very different magnitudes; Subtracting numbers of similar magnitudes; Multiplying and ...
Read more >
time - Is there concurrency or is it just missing precision? - Physics ...
Imagine I measure the time of two balls hitting the ground. My stopwatch displays 5.5 seconds for both of them. Then I could...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found