SyncFloatο
- class thread_factory.concurrency.sync_types.sync_float.SyncFloat(*args, **kwargs)[source]ο
Bases:
ISyncπ Core Purposeο
SyncFloat provides synchronized access to floating-point operations, ensuring that multiple threads can safely read and modify the value without race conditions.
π§ Featuresο
Full support for arithmetic operations (+, -, *, /, //, %, **, etc.)
Comparison operators (==, <, >, etc.)
Type conversion (int(), float(), str())
Manual and atomic value access via .get() and .set()
Safe thread-aware binary operations with locking on both operands
Cross-compatible operations with other Sync types (e.g., SyncInt, SyncBool)
Reverse operation support (__radd__, __rsub__, etc.)
CPython compatibility with as_integer_ratio, hex, and fromhex
π§ Deadlock Preventionο
For binary operations involving multiple Sync types, locks are always acquired in order of object ID to avoid deadlocks.
This makes operations like SyncFloat(3.14) + SyncFloat(2.0) safe across threads.
π Sync Type Interopο
The _unwrap_other() method allows flexible interaction between SyncFloat and:
Other SyncFloat, SyncInt, or any object with a get() method
Primitive types like float, int, str (auto-converted via float())
- β Recommended Usage:
a = SyncFloat(3.0) b = SyncInt(2) c = a + b # Thread-safe: unwrapped and locked d = SyncFloat(2.5) e = SyncFloat.safe_pow(a, d, 2) # Apply ternary pow safely (custom)
- as_integer_ratio()[source]ο
Return a pair of integers whose ratio is exactly equal to the float.
- Returns:
The numerator and denominator.
- Return type:
tuple[int, int]
- Raises:
OverflowError β If the float is infinity.
ValueError β If the float is NaN.
- conjugate()[source]ο
Return the complex conjugate of the float (always self for real numbers).
- Returns:
The same value.
- Return type:
float
- decrement(value: Real | ISync = 1.0)[source]ο
Atomically subtract value from the internal float and return the new total.
- static fromhex(s)[source]ο
- Parameters:
s (str) β A hexadecimal float string (e.g., β0x1.91eb851eb851fp+1β).
- Returns:
A new instance initialized with the parsed float.
- Return type:
- get() float[source]ο
Get the current float value in a thread-safe way.
- Returns:
The current value.
- Return type:
float
- hex()[source]ο
Return a hexadecimal string representation of the float.
- Returns:
Hexadecimal float string (e.g., β0x1.999999999999ap-4β).
- Return type:
str
- property imag: floatο
Since SyncFloat represents real numbers, this always returns 0.0.
- Returns:
0.0
- Return type:
float
- increment(value: Real | ISync = 1.0)[source]ο
Atomically add value to the internal float and return the new total.
- is_integer()[source]ο
Check if the float is equivalent to an integer.
- Returns:
True if value is an integer.
- Return type:
bool
- property real: floatο
This mirrors float behavior. For SyncFloat, itβs always equal to self.get().
- Returns:
The real part of the float.
- Return type:
float