samedi 29 octobre 2022

Formula returns -0 instead of INF in C++

I am new to C++ and am trying to figure out one thing: I am given this formula to count z with different values for m given. All good with most of the values except when m=0. I was expecting to receive INF for z, due to the division by 0 in this part 2 / sqrt(m) but received z= -0 instead. Is there any formal explanation to this? Because if we take z2 = 2 / sqrt(m) separately, it will result in inf. I need to write a report and have no idea on how to explain this, found nothing in C++ documentation. Using Microsoft Visual Studio Code 2017 if it matters. Would appreciate your help!

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#define _USE_MATH_DEFINES 
#include <math.h>
#include <ctgmath>

int main() {

    float m;
    cout << " Enter the 'm' value: ";
    cin >> m;
    float z;
    float z2;

    z = sqrt(pow((3 * m + 2), 2) - 24 * m) / (3 * sqrt(m) - 2 / sqrt(m));
    z2 = 2 / sqrt(m);
    cout << " \n When 'm' is " << m << ", 'z' value is: " << z << "; 'z2' value is: " << z2 << endl;


    return 0;
}

The console

tried to use double type instead of float, didn't help :(

Aucun commentaire:

Enregistrer un commentaire