0

So I am new to programming and am currently trying to make a program to calculate the amount of photons that are emitted from a metal by a specific wavelenght or frequency (photoelectric effect). The program is meant to use calculations with the frequency of an electromagnetic wave, which is why I am trying to define a function that, through simple division with c/λ, can calculate the frquency. However, when I tried to do this, Python gave me and answer that was off by 10^1. Why does this happen? The inserted values are supposed to be correct, and even my programming teacher couldn't find any mistake in the code.

Here's my code, by the way. I made this in a test file which I am planning on copying to my main .py document:

a = 550 #nm
c = 299792458 #m/s

b = a*(10**-9) #meter

print(b)
print(c)

d = c/b

print(d)

And this is the output that I get (the actual frequency should be around 54 077 200 000 000 (calculated on Google Calculator):

5.5e-07
299792458.0
545 077 196 363 636.3

As you can se, the answer is off by 10^1. How do I fix this?

  • By using `**-9`, you have entered the realm of floating point numbers. If you replace `10` with a `decimal.Decimal(10)`, you will get something like `545077196363636.3636363636364` instead. – Amadan Apr 08 '22 at 07:20
  • `float` can't keep all values so it keeps only aproximations. Display `f'{10**-9:.54f}` and you get `0.000000001000000000000000062281591457779856418897068693`. Most languages have this problem. You may have to use module `decimal` – furas Apr 08 '22 at 07:21

0 Answers0