For primitive type TypeFloat in terraform, when I write a acceptance test case where I pass a value called as 12.32 then it stores data as 12.31111**9

My acceptance test case fails as it’s not equal.

How to solve this ?

Can anyone help me in this

For primitive type TypeFloat in terraform, when I write a acceptance test case where I pass a value called as 12.32 then it stores data as 12.31111**9

My acceptance test case fails as it’s not equal.

How to solve this ?

Can anyone help me in this

Hi @RanzBiswa,

Because floating point numbers are stored in base-two rather than base-10, not all decimal fractions can be represented exactly. The imprecision you’re observing here is a typical result of a lossy conversion from base-10 to base-2 when no finite representation of the given number is possible in base-2.

Floating point numbers are supported in Terraform’s SDK mainly for completeness, and so they should be used only in very unusual circumstances where a binary floating point number is the physical representation required by the remote system that the provider is working with.

In most cases, it’s better to represent a fractional number as `TypeString`

and then either pass that string through verbatim to the underlying remote API (which can then convert it to a decimal number in whatever way it deems appropriate) or, in rarer cases, parse that number string within the provider code so that you can have more control over how exactly that conversion is done and what data type you will convert it to. (For example, it might be more appropriate to use a proper decimal fraction type, or a higher-precision binary floating point implementation.)

If you’re sure that floating point is really the best representation for the system you are integrating with, then your tests will need to accommodate the lossiness of converting to binary floating point. You could do that by selecting a number that has an exact, finite representation in both decimal and binary, or by retrieving the raw string from the Terraform state and parsing it into a float yourself using `strconv.ParseFloat`

, or by making your test *expect* the approximate value that results from converting that number to binary floating point.

1 Like