HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

What data type is better to use for a PostgreSQL to store number with a floating point and why?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlwhynumberwhatpointwithfloatingbetterstoretype

Problem

I have a Laravel application that needs to store minimum and maximum weight.
Currently, we are using integer values to store data but I need to modify it to store data values with a floating point.
What data type do I have to use for good accuracy?

Solution

I think that the REAL type should do the trick - if you're using kilos, you could have weights to the microgramme - unless you're using some fancy laboratory manipulations, that should be plenty - but weights like 4.567 kg (to the gramme) or 1234.45 kg (to the decigramme) would be fine.

From the manual here (Table 8.2):

real 4 bytes variable-precision, inexact 6 decimal digits precision

or, if you require more precision:

double precision 8 bytes variable-precision, inexact 15 decimal digits precision

and also:

The data types real and double precision are inexact,
variable-precision numeric types. On all currently supported
platforms, these types are implementations of IEEE Standard 754 for
Binary Floating-Point Arithmetic (single and double precision,
respectively), to the extent that the underlying processor, operating
system, and compiler support it.

This should be enough for any calculations involving weight where a couple of microgrammes here or there shouldn't really matter.

You could use one of the numeric types (NUMERIC or DECIMAL - essentially synonyms)

numeric variable user-specified precision, exact up to 131072 digits before the decimal point; up to 16383 digits after the decimal point

However, there is a (probably unnecessary) overhead associated with these:

However, calculations on numeric values are very slow compared to the integer types, or to the floating-point types described in the next section.

The upside of NUMERIC (or DECIMAL) is that:

The type numeric can store numbers with a very large number of digits.
It is especially recommended for storing monetary amounts and other
quantities where exactness is required. Calculations with numeric
values yield exact results where possible, e.g., addition,
subtraction, multiplication.

Obviously, your choice will depend on your (and/or your stakeholders') requirements - for weights, I recommend REAL.

One final idea would be to store weights as INTEGERS but in grammes (or mg or µg) depending on your requirements if computational power (essentially system RAM) is an issue - but I would still recommend one of the floating-point types.

-
If you are interesting in pursuing this further, then you could take a look at the site recommended in a different answer to your question (thanks to the anonymous responder). That site is What Every Programmer Should Know About Floating-Point Arithmetic and links therein.

-
A briefer explanation can be found here on the (excellent) SQLite site.

Context

StackExchange Database Administrators Q#325542, answer score: 5

Revisions (0)

No revisions yet.