Go Down

Topic: variables with decimals? (Read 606 times) previous topic - next topic

RudiAhlers

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

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

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



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

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

For what purpose?

RudiAhlers


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



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

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

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.

TanHadron

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

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