Pages: [1]   Go Down
Author Topic: Problems using System.println with a floar variable  (Read 280 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.

This is my test sketch:

Code:
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:
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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24298
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is printing the value of your float variable, the value of which is 0.00.

Code:
float b = 5.0/255.0;
or
Code:
float b = (float)5/(float)255;
  (though one "float" would be sufficient)
« Last Edit: November 14, 2012, 11:01:24 am by AWOL » Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot, that worked!

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

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Click on the plus sign next to the Karma figure.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
« Last Edit: November 14, 2012, 11:46:20 am by PaulS » Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 3991
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Examples can be found in your IDE.

Pages: [1]   Go Up
Jump to: