Converting Python to C: Struggling With Code

  • Thread starter swartzism
  • Start date
In summary: I have been able to replicate the code you provided, but I am still not understanding why it is doing what it does. Can you help me understand what is happening?In summary, the code is trying to input start and end as None, but if they are both None, it checks if start is 3 and end is self.num_atoms. If either of those values is not 3, it updates the coordinates using ztox().
  • #1
swartzism
103
0
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:
   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.
 
Technology news on Phys.org
  • #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
 
  • #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.
 
  • #4
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).
 
  • #5
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.
 
  • #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:
    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.
 

Related to Converting Python to C: Struggling With Code

1. Why would someone want to convert Python code to C?

There are a few reasons why someone might want to convert Python code to C. First, C is typically faster and more efficient than Python, so converting to C can improve the performance of the code. Additionally, C is a lower-level language and allows for more control over memory management, making it useful for developing high-performance applications.

2. What are some challenges that may arise when converting Python code to C?

One of the main challenges is that Python and C have very different syntax and programming styles. Python is a high-level language with a focus on readability and simplicity, while C is a low-level language with a focus on efficiency and control. This can make it difficult to translate code directly from one language to the other.

3. Are there any tools or libraries available to help with the conversion process?

Yes, there are a few tools and libraries that can assist with converting Python code to C. For example, Cython is a popular tool that can compile Python code into C or C++ code, which can then be compiled to native code. There are also libraries such as PyPy and Numba that use just-in-time (JIT) compilation to improve the performance of Python code.

4. Is it possible to convert any Python code to C?

In theory, it is possible to convert any Python code to C. However, the complexity and time required for the conversion process will vary depending on the code itself. Simple and straightforward Python code may be easier to convert, while more complex code with advanced features may be more difficult to translate to C.

5. Are there any alternatives to converting Python code to C?

Yes, there are a few alternatives to converting Python code to C. As mentioned earlier, using libraries like Cython, PyPy, and Numba can help improve the performance of Python code without needing to convert it to C. Another option is to use a hybrid approach, where some parts of the code are written in C and others in Python, to optimize performance and maintainability.

Similar threads

  • Programming and Computer Science
Replies
5
Views
10K
  • Programming and Computer Science
Replies
4
Views
4K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
889
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
8
Views
918
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
980
Replies
6
Views
1K
Back
Top