Why am I not getting correct result when I calculate exponent with ^ in C++?

后端 未结 5 1413
走了就别回头了
走了就别回头了 2020-12-12 02:39

I am using Bode\'s formuala to calculate distance of nth planet from sun

dist = (4 + 3*(2^(n-2)))/10

If I calculate the distance this way,

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-12 03:04

    Because ^ is not the power operator in C++. You can use std::pow but with double or you can do a lot of casting to make it work with int.

提交回复
热议问题