C++:洛伦兹因子方程

1 c++ physics

我是C++的新手.我参加了Python课程,但宁愿继续学习更有用的语言.但我正在用C++重做那个课程中的几个作业,以帮助我顺利完成.

这个特殊的问题是编写能解决Lorentz因子的代码,输入光速的一小部分(速度/ c)应该在0和1之间.我确信我有一个非常简单的事情.做错了; 来自Python的我可能不熟悉的东西.但帮助将不胜感激.我一直得到答案"南".它与我的类型声明有关吗?从我的理解,因为我正在使用小数,我应该使用浮动吗?

这是洛伦兹方程(但请记住,我的代码接受v/c作为一个数字):

http://spiff.rit.edu/classes/phys200/lectures/ke_rel/eqn_gamma.gif

#include <iostream>
#include <math.h>


using namespace std;

float lorentz_factor (float v) {
    float answer = 1 / sqrt(1 - exp(v));
    return answer;
}

int main() {

    float v;

    cout<<"Please enter a number between 0 and 1";
    cin>> v;
    while (!((v < 1) && (v > 0))) { // "v" should be entered as a fraction of     the speed of light.
        cout<<"Try again: ";
        cin>>v;                     // and only accepted if it is between 0 and 1
}
    float factor = v;
    cin.ignore();
    cout<<"The lorentz_factor is: "<< lorentz_factor (factor) << "\n";
    cin.get();
}  
Run Code Online (Sandbox Code Playgroud)

帮助我.

Joh*_*nck 7

你有:

1 / sqrt(1 - exp(v))
Run Code Online (Sandbox Code Playgroud)

但洛伦兹因子是:

1 / sqrt(1 - v*v)
Run Code Online (Sandbox Code Playgroud)