Skip to content

Converting float to int #8751

Closed
Closed
@mariusvds

Description

@mariusvds

Hello world,

This week i found a bug, converting float to int delivers strange values, can't find out why.

This is the code I wrote:

float answer = 0.0;
float i = 0.0;

void setup()
{
  Serial.begin(115200);
  Serial.println("Hello World !!!");
}

void loop()
{
  while(true)
  {
    answer = pow(5.0, i);
    Serial.print("Answer: ");
    Serial.println(answer);
    if (i >= 4.0){i = 0.0;}; //preventing overflow
    int b = static_cast<int>(answer);//converting to int
    Serial.print("B: ");
    Serial.println(b);
    delay(500);
    i += 1.0;
  }
}

The output is:
Hello World !!!
Answer: 1.00
B: 1
Answer: 5.00
B: 5
Answer: 25.00
B: 24
Answer: 125.00
B: 124

Somebody who can find out what goes wrong?

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: InvalidOff topic for this repository, or a bug report determined to not actually represent a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions