# floating-point

## Float storage precision in hardware only to 13 decimals

Float storage precision in hardware only to 13 decimals Question: There are many similar questions related to floating point, and I have read all of them that came up. Still, I am not able to grasp what I am doing wrong. Python3 Cython reference interpreter on 64-bit x86 machine stores floats as double precision, 8 …

## How to prevent Floating-Point errors with Pandas

How to prevent Floating-Point errors with Pandas Question: I have a problem with my Python code. I’m using pandas to read a Dataset and store it in a Data Frame. I’m now trying to convert ug to mg (1000ug == 1 mg) and g to mg (1000 mg == 1g). I’m first converting the Datatype of the …

## Why does timestamp() show an extra microsecond compared with subtracting 1970-01-01?

Why does timestamp() show an extra microsecond compared with subtracting 1970-01-01? Question: The following differ by 1 microsecond : In [37]: datetime(2514, 5, 30, 1, 53, 4, 986754, tzinfo=dt.timezone.utc) – datetime(1970,1,1, tzinfo=dt.timezone.utc) Out[37]: datetime.timedelta(days=198841, seconds=6784, microseconds=986754) In [38]: datetime(2514, 5, 30, 1, 53, 4, 986754, tzinfo=dt.timezone.utc).timestamp() Out[38]: 17179869184.986755 The number of microseconds in 986754 in …

## Why does math.cos(math.pi/2) not return zero?

Why does math.cos(math.pi/2) not return zero? Question: I came across some weird behavior by math.cos() (Python 3.11.0): >>> import math >>> math.cos(math.pi) # expected to get -1 -1.0 >>> math.cos(math.pi/2) # expected to get 0 6.123233995736766e-17 I suspect that floating point math might play a role in this, but I’m not sure how. And if …

## Python: How do I print a float with a configurable number of decimals

Python: How do I print a float with a configurable number of decimals Question: I want to print some numbers and easily configure how many decimals are displayed. How do I turn something like this: import numpy as np x, y, z, s = np.random.random(4) str_out = ‘[%0.4f,t%0.4f,t%0.4f,t%0.4f]’ % (x, y, z, s) print(str_out) and …

## How to maintain decimals when dividing with numpy arrays in Python

How to maintain decimals when dividing with numpy arrays in Python Question: So, I was working on implementing my own version of the Statsitical Test of Homogeneity in Python where the user would submit a list of lists and the fuction would compute the corresponding chi value. One issue I found was that my function …

## How to make that f"…" string formatting uses comma instead of dot as decimal separator?

How to make that f"…" string formatting uses comma instead of dot as decimal separator? Question: I tried: import locale print(locale.locale_alias) locale.setlocale(locale.LC_ALL, ”) locale.setlocale(locale.LC_NUMERIC, "french") print(f"{3.14:.2f}") but the output is 3.14 whereas I would like 3,14. How to do this with f"…" string formatting? Note: I don’t want to use .replace(".", ",") Note: I’m looking …

## Floating point arithmetic and comparison causing undesirable output

Floating point arithmetic and comparison causing undesirable output Question: Consider the Pandas DataFrame df1: df1 = pd.DataFrame({"Name":["Kevin","Peter","James","Jose","Matthew","Pattrick","Alexander"],"Number":[1,2,3,4,5,6,7],"Total":[495.2,432.5,’-‘,395.5,485.8,415,418.7],"Average_old":[86.57,83.97,’-‘,96.59,84.67,83.10,83.84],"Grade_old":[‘A’,’A’,’A’,’A+’,’A’,’A’,’A’],"Total_old":[432.8,419.8,’-‘,482.9,423.3,415,418.7]}) I calculated the Average and Grade with the following formula df1["Average"] = df1["Total"].apply(lambda x: x/5 + 0.1 if x != "-" else "-") df1["Grade"] = df1["Average"].apply((lambda x:’A+’ if x!=’-‘ and x>90 else ‘A’)) So df1 becomes df1 Name …