site banner
Unable to load image

I dont get good answers on google. Does anyone know why there is a computational error when a string is used to store a numerical value then it is converted to a number data type and it produces an error in the value?

I remember having a discussion with one of my math peers about floiting point error but it seems different than that. For example in c++

#Include <iostream></iostream>

#Include <cmath></cmath>

Using namespace std

Int main ()

{

\ declaration of string value

string X=" "

\ ui to get an input

cout << "Enter a numerical value";

cin >> X;

cout << "\n" << endl;

*conversion, redeclaration with associated values *\

const double pi =3.1415

double stragcel = stof(X) * pi * 2

\ print of your cute twinkry

cout << "The amount of cute twinkry in your soy titties is " << stragcel << " inches squared. You might wanna get that checked out." << " In terms of the range of the oscillations of your cute twinkry it is " << sin(stragcel) << " to " << sin(-stragcel) << ". This is dangerously off the charts. We cannot help your cute twinkry, sir." << endl;

return 0;

}I have done similar things with floating points but the error seems to be constant with int, float, and double. What is it?

11
Jump in the discussion.

No email address required.

It's just floating point error. Try stod instead of stof. The latter is giving you a single-precision float which only has a few decimal digits of precision.

  • 4
Jump in the discussion.

No email address required.

Must be why there was such and error with decimal values longer than 5 bits

  • 4
Jump in the discussion.

No email address required.