Go Down

Topic: Problems using System.println with a floar variable (Read 417 times) previous topic - next topic

dan_plan

Hi.

This is my test sketch:

Code: [Select]
void setup()
{
  Serial.begin(9600);
 
  float a = 1.23;
  Serial.println(a); 
 
  float b = 5/255;
  Serial.println(b);
}

void loop()
{
}


This is its output:
Code: [Select]

1.23
0.00


Can you please help me understand why it is printing 0.00 ?  How can I print my float variable?

Thanks,
Dan

AWOL

#1
Nov 14, 2012, 04:59 pm Last Edit: Nov 14, 2012, 05:01 pm by AWOL Reason: 1
It is printing the value of your float variable, the value of which is 0.00.

Code: [Select]
float b = 5.0/255.0;
or
Code: [Select]
float b = (float)5/(float)255;  (though one "float" would be sufficient)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

dan_plan

Thanks a lot, that worked!

How can I vote for your answer so to increase your karma?

dxw00d

Click on the plus sign next to the Karma figure.

dan_plan

I can't see it - probably I am too new to the forum for being able to vote :-(

PaulS

#5
Nov 14, 2012, 05:39 pm Last Edit: Nov 14, 2012, 05:46 pm by PaulS Reason: 1
Quote
I can't see it - probably I am too new to the forum for being able to vote :-(

I voted for you.

Above AWOL's avatar, there is a karma listing. The little green thing is what you click to bump his karma (or dogma).

GoForSmoke

When your karma runs over somebody's dogma, it's nice to say you're sorry.

I've been here more than year and now I find out what those things on the forum mean. If I had known from the start then some-a-yuns would have a lot more.

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Go Up