Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Custom Numeric Types: Wrap around number

Tags:

python

types

Quick one hopefully; brain has gone blank.

Is it possible / practical to have a class that is just a generic numeric value (eg float, int, etc) that when a value becomes <=0 it loops around to os.maxint-value, similar to a unsigned value in C?

'This is completely insane' is also an acceptable answer!

'Reasonable' use cause; a particular 'effort' cost evaluation where a negative 'cost' is, while theoretically fine, practically invalid and is usually checked for each value and flipped to maxint if <=0.

Instead of all those lovely additional comparisons, just give the cost-value this inate behaviour.

like image 334
Bolster Avatar asked Jan 28 '26 16:01

Bolster


1 Answers

Found the answer

Using Numpy's uint types works for this.

import numpy.uint32
valueA=uint32(5)
valueB=uint32(-5)
assert valueA < valueB
like image 77
Bolster Avatar answered Jan 30 '26 06:01

Bolster