Double or float assignment in C++

The below works as a double, but not as a float. I can't figure out where to put the f to make float work.

int main()
{
    double TwelveRootTwo{ pow(2,1 / 12.00) };
   std::cout << "TwelveRootTwo = " << TwelveRootTwo << '\n';
}

In what way does it not work when you use float?

Replace double with float.

Since this is an Arduino forum, are you using a platform where float and double are different? On ESP32, where they are, consider

  auto flo = pow(2, (1/12.0f));
  auto dou = pow(2, (1/12.00));
  Serial.println(flo, 15);
  Serial.println(dou, 15);
  Serial.println(1/12.0f, 15);
  Serial.println(1/12.00, 15);
  Serial.println(sizeof(flo));
  Serial.println(sizeof(dou));
  auto ff3 = pow(2.0f, (1.0f/12.0f));
  Serial.println(sizeof(ff3));
  Serial.println(ff3, 15);

prints

1.059463096183108
1.059463094359295
0.083333335816860
0.083333333333333
8
8
4
1.059463143348694

No, that's what you did. He's asking what happened. In what way was it broken? What did you expect to happen? What actually happened? Compare and contrast the two.

This^^

I knew I needed to add an f to something... just wasn't sure what.
float TwelveRootTwo{ pow(2.0f,1.0f / 12.0f) };
Thanks!

1 Like