iontom
iontom

Reputation: 473

Most efficient way to store scientific notation in Python and MySQL

So I'm trying to store a LOT of numbers, and I want to optimize storage space.

A lot of the numbers generated have pretty high precision floating points, so: 0.000000213213 or 323224.23125523 - long, high memory floats.

I want to figure out the best way, either in Python with MySQL(MariaDB) - to store the number with smallest data size.

So 2.132e-7 or 3.232e5, just to basically store it as with as little footprint as possible, with a decimal range that I can specify - but removing the information after n decimals.

I assume storing as a DOUBLE is the way to go, but can I truncate the precision and save on space too?

I'm thinking some number formating / truncating in Python followed by just normal storage as a DOUBLE would work - but would that actually save any space as opposed to just immediately storing the double with N decimals attached.

Thanks!

Upvotes: 2

Views: 489

Answers (2)

ShpielMeister
ShpielMeister

Reputation: 1455

if, on the other hand, you are trying to minimize the representation of numbers for say transmission via json or xml, you could use f-strings.

>>> from math import pi
>>> pi
3.141592653589793

>>> f'{pi:3.2}.'
 '3.1.'
>>> bigpi = pi*10e+100
>>> bigpi
 3.141592653589793e+101

>>> f'{bigpi:3.2}'
 '3.1e+101'

Upvotes: 0

ShpielMeister
ShpielMeister

Reputation: 1455

All python floats have the same precision and take the same amount of storage. If you want to reduce overall storage numpy arrays should do the trick.

Upvotes: 1

Related Questions