Decimal variable precision

Is there a smaller variable type than float that supports decimal? I am looking for the smallest variable to have the precision of (00.000 - 99.999)

Float and double are the same on Spark.

You could use a 32-bit unsigned integer so that 99999. means 99.999 scaled by 1000. You just have to present it to the user eventually in a scaled fashion.

That is a lot of precision for a real-world value–maybe you could get by with less? Dropping one significant digit would let you use uint16_t scaled by 100.

1 Like

I am clocking response time of a shooter on an obstacle course. They could take up to 30 seconds to finish the coarse and we want the response times to be in milliseconds.

So I would use the difference between two calls to micros() which returns an unsigned long (uint32_t) type.

You then have to format it nicely for the user. If you are sending it to a web page, I would just format in Javascript on the web side. For an LED, the decimal point is just constant. For an LCD, you would use the modulus operator % and the integer divide operator / to get the fractional and integer parts.

1 Like

@bko, this is unrelated to the current conversation, but I thought that float was 32-bit and double was 64-bit on the core? If not, someone needs to update the docs page to clarify that.

1 Like

Hi @mumblepins

You are right! I just checked sizeof() and floats are 4 bytes and doubles are 8 bytes.

I stand corrected–thank you!