4

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?

0

2 Answers 2

6

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.

0
3

It depends on your requirements regarding correctness for math operations. Floating point values are approximate values: what you store might not be what you retrieve. For more details see What Every Programmer Should Know About Floating-Point Arithmetic

If correctness is important use numeric. If performance on millions or hundreds of millions of rows is more important then use a floating point type. - user1822

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.