Вопрос:

Why does numbers like this: -1.7e+007 appear in the results? | C++

c++

52 просмотра

2 ответа

9 Репутация автора

Can someone explain me why does this strange numbers appear on the compiler?

This is how it should appear (numbers I entered):

The addition of 10000000+12000000+15000000 is: XXXXX

The substraction of 10000000-12000000-15000000 is: XXXXX

The division of 10000000/12000000/15000000 is: XXXXX

The multiplication of 10000000*12000000*15000000 is: XXXXX

The operation of 10000000*12000000+15000000-10 is: XXXXX

And this is what it shows me:

The addition of 1e+007+1.2e+007+1.5e+007 is: 3.7e+007

The substraction of 1e+007-1.2e+007-1.5e+007 is: -1.7e+007

The division of 1e+007/1.2e+007/1.5e+007 es: 5.55556e-008

The multiplication of 1e+007*1.2e+007*1.5e+007 is: 1.8e+021

The operation of 1e+007*1.2e+007+1.5e+007-10 is: 1.2e+014

This is the code:

#include <sstream>
#include <iostream>
#include <math.h>
using namespace std;

class Test
{
private:
    float number1, number2, number3;
public:
    Test();
    Test(float, float, float);
    ~Test();

    void setNumber1(float);
    void setNumber2(float);
    void setNumber3(float);

    float getNumber1();
    float getNumber2();
    float getNumber3();

    float additionNumbers();
    float substractionNumbers();
    float divisionNumbers();
    float multiplicationNumbers();
    float operationNumbers();

    string toString();
};

Test::Test()
{
    number1=0;
    number2=0;
    number3=0;
}

Test::Test(float pnumber1, float pnumber2, float pnumber3)
{
    number1=pnumber1;
    number2=pnumber2;
    number3=pnumber3;
}

Test::~Test()
{

}

void Test::setNumber1(float pnumber1)
{
    number1=pnumber1;
}

void Test::setNumber2(float pnumber2)
{
    number2=pnumber2;
}

void Test::setNumber3(float pnumber3)
{
    number3=pnumber3;
}

float Test::getNumber1()
{
    return number1;
}

float Test::getNumber2()
{
    return number2;
}

float Test::getNumber3()
{
    return number3;
}

float Test::additionNumbers()
{
    float addition=0;

    addition=getNumber1()+getNumber2()+getNumber3();

    return addition;
}

float Test::substractionNumbers()
{
    float substraction=0;

    substraction=getNumber1()-getNumber2()-getNumber3();

    return substraction;
}

float Test::divisionNumbers()
{
    float division=0;

    division=getNumber1()/getNumber2()/getNumber3();

    return division;
}

float Test::multiplicationNumbers()
{
    float multiplication=0;

    multiplication=getNumber1()*getNumber2()*getNumber3();

    return multiplication;
}

float Test::operationNumbers()
{
    float operation=0;

    operation=getNumber1()*getNumber2()+getNumber3()-10;

    return operation;
}

string Test::toString()
{
    stringstream s;

    s<<"The addition of "<<getNumber1()<<"+"<<getNumber2()<<"+"<<getNumber3()<<" is: "<<additionNumbers()<<endl;
    s<<"The substraction of "<<getNumber1()<<"-"<<getNumber2()<<"-"<<getNumber3()<<" is: "<<substractionNumbers()<<endl;
    s<<"The division of "<<getNumber1()<<"/"<<getNumber2()<<"/"<<getNumber3()<<" is: "<<divisionNumbers()<<endl;
    s<<"The multiplication of "<<getNumber1()<<"*"<<getNumber2()<<"*"<<getNumber3()<<" is: "<<multiplicationNumbers()<<endl;
    s<<"The operation of "<<getNumber1()<<"*"<<getNumber2()<<"+"<<getNumber3()<<"-10 is: "<<operationNumbers()<<endl;

    return s.str();
}

int main()
{
    float n1=0, n2=0, n3=0;

    Test numbers1(n1, n2, n3);

    cout<<"Enter the first number: ";
    cin>>n1;
    numbers1.setNumber1(n1);

    cout<<"Enter the second number: ";
    cin>>n2;
    numbers1.setNumber2(n2);

    cout<<"Enter the third number: ";
    cin>>n3;
    numbers1.setNumber3(n3);

    cout<<numbers1.toString();

    return 0;
}
Автор: Zanduish Источник Размещён: 11.08.2019 08:08

Ответы (2)


0 плюса

26 Репутация автора

Решение

Это стандартная форма отображения длинных недискретных значений. (Это как std::coutили printfработа.)

Автор: Nestor Размещён: 11.08.2019 08:14

0 плюса

310898 Репутация автора

Это называется научным обозначением .

По умолчанию потоки ввода / вывода выбирают для вас выходную нотацию с плавающей запятой, в зависимости от значений.

Вы можете изменить это, используя Манипуляторы ввода / вывода. Например, std::fixed.

std::stringstream s;
s << std::fixed;

s << "The addition of " << // etc ... 
Автор: Lightness Races in Orbit Размещён: 11.08.2019 10:51
Вопросы из категории :
32x32