Go Down

### Topic: variables with decimals? (Read 1 time)previous topic - next topic

#### RudiAhlers

##### Jun 24, 2013, 09:19 pm
Hi,

I am trying to use a variable with a decimal place, but can't.

The "float" variable option is the only option that allows me to use a decimal, but it's big (use 4 bytes) and isn't always exact (Source: http://arduino.cc/en/Reference/Float)

I would expect use a decimal place with "word" but can't either.

Can anyone tell me if it's at all possible to use a decimal in a variable, reliably?

A simple sketch would be:

Code: [Select]
`int Decimal1 = 0.1;word Decimal2 = 0.2;float Decimal3 = 0.3;void setup(){  Serial.begin(9600);  Serial.println(Decimal1);  Serial.println(Decimal2);  Serial.println(Decimal3);}void loop(){}`

The output is:

Quote

0
0
0.30

#### PaulS

#1
##### Jun 24, 2013, 09:25 pm
Quote
I would expect use a decimal place with "word" but can't either.

Nothing we can do about unreasonable expectations. word is some crap invented by the idiots in Redmond to describe an int.

Quote
Can anyone tell me if it's at all possible to use a decimal in a variable, reliably?

Of course. That's what floats are for. Well, besides parades.

Quote
The "float" variable option is the only option that allows me to use a decimal, but it's big (use 4 bytes) and isn't always exact (Source: http://arduino.cc/en/Reference/Float)

It's 4 bytes, yes. It has 6 to 7 digits of accuracy. Isn't the sufficient? Can you tell the difference in temperature between 27.003 and 27.0031 degrees?

#### KeithRB

#2
##### Jun 24, 2013, 09:26 pm
You can't. Unless you create your own fixed decimal type, you are stuck with two options:
Use float: Accuracy "and isn't always exact " is usually OK in practice.
Use an integer and treat the last two (or whatever) digits as decimals. i.e., x = 102 would really be 1.02.

#### RudiAhlers

#3
##### Jun 24, 2013, 09:37 pm

Use an integer and treat the last two (or whatever) digits as decimals. i.e., x = 102 would really be 1.02.

Thanx, that's probably going to be the safest then. But, how do I add the decimal point after the first character?

#### PaulS

#4
##### Jun 24, 2013, 10:00 pm
Quote
But, how do I add the decimal point after the first character?

For what purpose?

#### RudiAhlers

#5
##### Jun 24, 2013, 10:10 pm

Quote
But, how do I add the decimal point after the first character?

For what purpose?

To convert, for example, 102 to 1.02.

Or 14920 to 1.4920

#### Arrch

#6
##### Jun 24, 2013, 10:41 pm

Quote
But, how do I add the decimal point after the first character?

For what purpose?

To convert, for example, 102 to 1.02.

Or 14920 to 1.4920

Again for what purpose? What are you going to do with the floating point number that you can't do with the integer version of it.

#### KeithRB

#7
##### Jun 24, 2013, 11:27 pm
The only time it is an issue is when you print it. You will need to create a special "PrintMyVariable" function that adds the decimal point and return a string to be printed.

#### JarkkoL

#8
##### Jun 24, 2013, 11:42 pm
If you want more compact representation of real numbers you could use fixed point or 16-bit float formats. There is no hardware support for these per se, though fixed point operates with standard integer math and you just convert to/from regular float from it. So if you have say 10.6 fixed point value (i.e. 16 bits) it means you allocate 10 bits for integer and 6 bits for decimal part and store the entire thing in one 16-bit value. In 10.6 case your integer has 2^10 values and decimal part 2^6 values so int range is [-512, 511] (or [0, 1023] for unsigned) and decimal precision of 1/(2^6)=~0.0156. So based on your requirements you can allocate enough bits for int & decimal parts. 16-bit float is more tricky but it can handle wider range of values with varying precision just like regural floats.

#9
##### Jun 25, 2013, 07:32 am
Quote
Thanx, that's probably going to be the safest then. But, how do I add the decimal point after the first character?

That's going to depend on how you're displaying it.  If you're sending it to Serial.print(), you can do some quick math:

Code: [Select]
`  int x;  x = 102;      // Really 1.02, but we're keeping it stored as x * 100 so it fits in an int and doesn't lose decimal places  Serial.print(x / 100);  Serial.print(".");  if (x % 100 < 10)    Serial.print("0");  Serial.println(x % 100);`

#### KeithRB

#10
##### Jun 25, 2013, 04:03 pm
You do have to be careful with multiplying and dividing to keep track of the decimal. Some work with pencil and calculator should show you what to do.

Go Up

Please enter a valid email to subscribe