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:

SyncFloat

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

set(new_value: Real | str)[source]

Set a new float value in a thread-safe way.

Parameters:

new_value (Real | str) – A numeric or string value convertible to float.