CKTIS / Python
math.fsum
Somma numeri in virgola mobile mantenendo più somme parziali.
Versione: Python 3.14; repository image, production runtime not attested
Sintassi
math.fsum(iterable, /)Somma numeri in virgola mobile mantenendo più somme parziali.
A 1e16 l’incremento di 1,0 si perderebbe in una singola addizione. Il resto conservato dà 1,0 dopo aver sottratto 1e16.
Un ingresso vuoto dà 0,0.
import math
assert (1e16 + 1.0) - 1e16 == 0.0
assert math.fsum([1e16, 1.0, -1e16]) == 1.0 and math.fsum([]) == 0.0
Esempi
import math
print(math.fsum([1e16, 1.0, -1e16]))
print(math.fsum([]))
Risultato
1.0 0.0
Un ingresso vuoto dà 0,0.