Trying to make a basic Wavelength/Frequency converter, large values are problematic

StackOverflow https://stackoverflow.com/questions/18886045

  •  29-06-2022
  •  | 
  •  

Pergunta

I'm trying to make a basic conversion program in Python to calculate wavelength of a photon when given the frequency (and vice versa) since my calculator is really frustrating to work with. It worked fine for my first calculation:

Convert to frequency or wavelength? (hz/wl) hz
What is the wavelength? 7.24e-07
414364640883977.0

But when I tried to convert into wavelength and gave it a frequency of 4.80e15, it simply returned "0.0":

Convert to frequency or wavelength? (hz/wl) wl
What is the frequency? 4.80e015
0.0

Is there a problem somewhere? I imported math and numbers just in case that might fix something but it didn't seem to help.

def converter():
    import numbers
    import math
    conversion = input("Convert to frequency or wavelength? (hz/wl) ")

    if conversion == "hz":
        wl = eval(input("What is the wavelength? "))
        c = 3.00e08
        hz = c // wl
        print(hz)
    if conversion == "wl":
        hz = eval(input("What is the frequency? "))
        c = 3.00e08
        wl = c // hz
        print(wl)

converter()

I tried with several other values and also decreasing the scale overall (making everything 10^8 less) but that didn't change anything.

Foi útil?

Solução

// is floor division in Python. Surely you want floating division?

>>> 3e8 // 4.8e15  # returns the floor
0.0
>>> 3e8 / 4.8e15
6.25e-08

In other words, replace // by / in your code. I bet you'll be happier ;-)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top