0

I am currently working on a code to find a value C which I will then compare against other parameters. However, whenever I try to run my code I receive this error: ValueError: math domain error. I am unsure why I am receiving this error, though I think it's how I setup my equation. Maybe there is a better way to write it. This is my code:

import os
import math


path = "C:\Users\Documents\Research_Papers"

uH2 =5
uHe = 3
eH2 = 2
eHe = 6

R = ((uH2*eH2)/(uHe*eHe))

kH2=[]
kHe=[]

print(os.getcwd()) # see where you are
os.chdir(path) # use a raw string so the backslashes are ok
print(os.getcwd()) # convince yourself that you're in the right place
print(os.listdir(path)) # make sure the file is in here
myfile=open("hcl@hfs.dat.txt","r")
lines=myfile.readlines()

for x in lines:
    kH2.append(x.split(' ')[1])
    kHe.append(x.split(' ')[0])
myfile.close()
print kH2
print kHe

g = len(kH2)
f = len(kHe)
print g
print f

for n in range(0,7):
    C = (((math.log(float(kH2[n]),10)))-(math.log(float(kHe[n]),10)))/math.log(R,10)
    print C

It then returns this line saying that there is a domain error.

C = (((math.log(float(kH2[n]),10)))-(math.log(float(kHe[n]),10)))/math.log(R,10)

ValueError: math domain error

Also, for the text file, I am just using a random list of 6 numbers for now as I am trying to get my code working before I put the real list of numbers in. The numbers I am using are:

5 10 4 2
6 20 1 2
7 30 4 2
8 40 3 2
9 23 1 2
4 13 6 2
Cosmoman
  • 101
  • 1
  • 12
  • 2
    Debugging tip - break your complicated line down into pieces until you isolate the error. – enderland Oct 11 '16 at 03:08
  • I suspect you are using Python 2. If so, then `R` will be 0. Change `R = ((uH2*eH2)/(uHe*eHe))` to `R = float(uH2*eH2)/(uHe*eHe)`. See, for example, [this question](http://stackoverflow.com/questions/2958684/python-division) for details. – Warren Weckesser Oct 11 '16 at 03:09
  • Warren it seems to work now with your suggestion. Thanks for your advice. Also, thanks for your advice enderland. – Cosmoman Oct 11 '16 at 03:16

1 Answers1

0

Try to check if the value inside the log is positive as non-positive value to a log function is a domain error.

Hope this helps.

pacholik
  • 8,607
  • 9
  • 43
  • 55
Harv
  • 446
  • 5
  • 12