Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Converting Py to C

  1. Mar 27, 2013 #1
    I'm in the process of converting some Python code to C and have come across a structure I'm not sure on how to translate:

    (This is a hybrid of the two languages)
    Code (Text):
       try:
          return(acos(ang)*RAD_2_DEG);
       except ValueError:
          if (ang < -1.0)
             return(180.0);
          else
             return(0.0);
    Does anyone know how to get this fully to C? It is trying to return acos(ang)*RAD_2_DEG and if it catches ValueError, it then checks if ang is less than -1.0 and returns a value of 180 or 0 based on that. Any help would be appreciated.

    Thanks in advance.
     
  2. jcsd
  3. Mar 27, 2013 #2
    Hhhmmm...I am not C expert, but just don't try to convert the code literally, line by line, just go for the overall objective.

    what I am saying, it's that maybe you should start by doing the validation of ang, in the first place; if it meets the "failing" criteria, you take the shortcut to return something, if it passes the tests and it is then safe to use acos() on it, then you do that last
     
  4. Mar 27, 2013 #3
    Roger that. I found a similar snippet of code that was doing the same thing, but much more clearly. I got it converted successfully. Thanks for the tip though.
     
  5. Mar 27, 2013 #4

    Mark44

    Staff: Mentor

    IIRC, C doesn't have try...catch blocks, although C++ and C# do.

    As gsal suggested, rather than completely duplicate the code you have, just do the checks before you call acos().

    BTW, the argument to acos() is not an angle - it's a number in the interval [-1, 1]. If the argument is not in this interval, you should not call acos(); otherwise an error will be produced.

    The result from acos() is an angle in radians (which I believe you already know).
     
  6. Mar 27, 2013 #5

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    In the original Python, The acos() function will throw a "value error" if its argument is > 1 or < -1. If that happens, you do what is in the "except" part. Otherwise, what you "try" to do will be successful.

    As gsal said, the easiest way to do this in C is test the argument yourself before you call acos().

    The standard C library does have some very primitive error-catching capabilities, but the details are implementation dependent, so it's usually easier to code your own checks rather than try to use them.

    The C++ language has "try" and "catch" which works in a similar way to Python.
     
  7. Mar 28, 2013 #6
    Guys,

    Thank you for the input. I did not think of the nature of acos() being between [-1,1], that is very helpful. I have come across something that baffles me in the same arena:

    Code (Text):
        def update_coordinates(self, start=None, end=None, ztox=Atom3d.ztox):

           // Compute cartesian coordinates from internal coordinates
           //
           // Arguments
           //
           //    o *start* - integer
           //
           //    o *end* - integer
           
            if start == end == None:
                ztox(self.atoms)
            else:
                if start is None: start = 3
                if end is None: end = self.num_atoms
                ztox(self.atoms, start, end)
    This seems to input start and end as None, check whether they are None in an if-else statement where both if and else are satisfied since start and end are read in as None. Am I missing something?

    Again, thank you for the help.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook