1

I'm designing an algorithm in python and know I'll want to translate it to C later.

However, mathematical operations in Python might not yield the same result as in C, for instance 4294967295 + 1 = 0 in C for unsigned integers, but not with plain Python integers operations. Therefore, I should not use Python integers in my design.

Can I safely and easily use Numpy to reproduce C behavior ? That is, if I perform usual operations (+, -, *, /, %, casting from float to int or the other way around) on arrays with types np.uint32 or np.float64 for instance, am I guaranteed (or can I get this guarantee somehow) to get the same result as a C program with uint32_t and float64_t ?

I'm only interested of what's part of the C "official behavior", anything that is allowed to depend on the compiler or processor in C can also differ with numpy as if it was another compiler/processor. I'm asking in particular since numpy has a Nan that is not always in C.

EDIT after comments :

I'm looking more particularly at this set of operations : (+, -, *, /, %, casting from float to int or the other way around).

I've tried to look at numpy documentation to no avail, and have run a few tests myself, for instance :

TEST 1 : int32 overflow ((uint32_t) 4294967295 + (uint32_t) 1 == 0 in C)

It does not seem to work with numpy scalars

>>> import numpy >>> a = numpy.uint32(4294967295) >>> type(a) <class 'numpy.uint32'> >>> a += 1 >>> a 4294967296 >>> type(a) <class 'numpy.int64'> 

But it does with numpy arrays :

import numpy a = numpy.array([4294967295], dtype='uint32') a += 1 print(a) print(a.dtype) 

Output:

[0] uint32 

But this specific case does not give me any insurance that it always works with arrays.

**TEST 2 : negative integer division : **

-1/2 == 0 in C for int32.

But in "plain" numpy :

two = np.int64(2) mone = np.int64(-1) print(mone / two) print(mone // two) 

Gives :

-0.5 -1 

I'm wondering whether there is some kind of "switch" to numpy, or operands that I could use, so that numpy would give me 0 in the above case for instance

8
  • 2
    Pretty broad question. You didn't even list the operations you want to compare. You didn't even attempt to compare them yourself. Please ask specific questions. Commented Mar 18, 2024 at 14:47
  • 1
    Re "Can I safely use Numpy to reproduce C behavior ?", Well, you just said you couldn't use Python integers to reproduce C behaviour, which is clearly false. Python is a full programming language. It can produce any behaviour you want. So how can we possibly answer this question? Commented Mar 18, 2024 at 14:49
  • 2
    Re "numpy has a Nan that is not in C", C has NaN if the machine does. x86/x86_64 uses IEEE floating-point numbers, so it has NaN Commented Mar 18, 2024 at 14:49
  • @ikegami 1. I've added specifications for the operations I want, and tests. 2. "Python is a full programming language. It can produce any behaviour you want" : that's right, I'll add "easily" in my question, I'm looking for an answer that does not involve re-coding C typing and operations myself, obviously ;). 3. " "numpy has a Nan that is not in C", C has NaN if the machine does" : I'm indeed not very familiar with this point, thanks for the explanation Commented Mar 18, 2024 at 15:44
  • 3
    Modulo on negative numbers in particular is different - modulo in NumPy implements Python's behavior for modulo, but C implements C's behavior. stackoverflow.com/questions/1907565/… Commented Mar 18, 2024 at 15:50

1 Answer 1

0

It looks like the simple answer is no

  • divisions of negative integers behaves differently :

-1/2 == 0 in C for int32.

But in "plain" numpy :

two = np.int64(2) mone = np.int64(-1) print(mone / two) print(mone // two) 

And there seems to be no "switch" or alternative operands built in numpy to reproduce the C behavior

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.