Float to int

Can someone explain to me the whole idea of float and int? I understand that int are whole numbers, and float are decimal numbers, but why can't one turn into the other? It's a homework code that I have to debug, but I'm so confused.

Can you post the code?
...... but why can't one turn into the other?


Well you can, you just have to be careful in which direction you do it in.

1
2
3
4
5
6
7
double a = 1.3;
int b = a;  // b is 1, truncation for int. Attracts warning for the implicit truncation.

b = static_cast<int>(a); // explicit cast to int, no warning, prefer to do this always.

int c = 2;
double d = c; // d is 2.0, no warning, we are going to a wider type, no loss of precision 
Sorry, this is the code that I'm suppose to debug. Line 24 is the first error, but I don't understand why.

// Week 1 Assignment-2
// Description: Numbers and conversions with simple variables
//----------------------------------

//**begin #include files************
#include <iostream> // provides access to cin and cout
#include <iomanip>
//--end of #include files-----------
//----------------------------------

using namespace std;
//----------------------------------

//**begin main program**************
int main()
{
// create and initialize variables
int x, y;
float f1 = 1.6767676f;
float f2 = 2.2f;
float diff;
// demonstrate math and number output
x = y;
x = f1; // What happens when you store a float into an int?
y = f2; // Does it round or truncate?
// display ints
cout << "--->using default cout settings: " << endl;
cout << "x=" << x << " y=" << y << endl;
cout << "f1=" << f1 << " f2=" << f2 << endl;

cout << "--->using scientific settings: " << endl;
cout << scientific << "x=" << x << " y=" << y << endl;
cout << scientific << "f1=" << f1 << " f2=" << f2 << endl;
cout << "--->setting precision to 4" << setprecision(4) << endl;
cout << scientific << "x=" << x << " y=" << y << endl;
cout << scientific << "f1=" << f1 << " f2=" << f2 << endl;
cout << "--->using fixed settings: " << endl;
cout << fixed << "x=" << x << " y=" << y << endl;
cout << fixed << "f1=" << f1 << " f2=" << f2 << endl;
cout << "--->setting precision to 12" << setprecision(12) << endl;
cout << fixed << "x=" << x << " y=" << y << endl;
cout << fixed << "f1=" << f1 << " f2=" << f2 << endl;
cout << "--->restoring float (default) settings: " << endl;
cout << defaultfloat << "x=" << x << " y=" << y << endl;
cout << "f1=" << f1 << " f2=" << f2 << endl;
cout << "--->setting precision to (default) 6" << setprecision(6) << endl;
cout << "x=" << x << " y=" << y << endl;
cout << "f1=" << f1 << " f2=" << f2 << endl;
cout << "-----> Showing results of unusual and unexpected math operations " << endl;
f2 = sqrt(f1);// take the square root of f1
cout << "The square root of " << f1 << " is " << f2 << endl;
f2 = f2*f2; // square the square root of f1
cout << "Squaring f2 should give us the same value as f1. f1 = " << f1 << " f2 = " << f2 << endl;
diff = f1 - f2; // This should be zero--is it?
cout << "diff should be zero. diff = " << diff << endl;
cout << "Oops. Let's take another look at f1 and f2 in higher precision." << endl;
cout << "--->setting precision to 12 places. f1 = " << setprecision(12) << f1 << " f2 = " << f2 << endl;
f2 = f2 / diff; // What happens when we divide a floating point by something close to zero?
cout << "Division by a very small number. f2 = " << f2 << endl;
f1 = f1 / 0.0; // What happens when we actually divide by zero?
cout << "Division by zero. f1 = " << f1 << endl;
f1 = f1 * diff;
cout << "Multiply a small negative number by infinity. f1 = " << f1 << endl;

// Wait for user input to close program when debugging.
cin.get();
return 0;
}
//--end of main program-------------
//----------------------------------
Firstly :
f1 = f1 / 0.0; // What happens when we actually divide by zero?

Don't try it. Dividing any number by zero always results in a program undefined behavior.
Last edited on
Please always use code tags :+)

http://www.cplusplus.com/articles/z13hAqkS/

Amongst other things it provides line numbers.

Line 24 is the first error, but I don't understand why.


I explained that in my earlier post :+)
Last edited on
OK, I think I understand now. Just for clarification though, why double not float? Does it have to do with how much data it can hold?
I always prefer double and it is the default for literals in C and C++. A float can only do 6 or 7 significant figures, while double can do 15 or 16, depending on the implementation. So the precision of float can be easily exceeded. Only use float if some library requires it (typically a graphics library ) or if you have billions of them and possibly have some plans to cope with normalising them. long double can do 18 or 19sf

Edit:

The other thing to consider: There are lots of numbers (including whole numbers) that don't convert exactly to Floating Point (FP, float, double, or long double) I am fairly sure this happens more often with float than with double - the last sf is a bigger binary fraction with the float.
Last edited on
Can I fix the code by calling the int in Line 18 a double? Or at least fix my first error?

Edit: Never mind, I still get an error trying to do that too. Does coding get easier? I understand now why I get the error, but have no idea how to go about fixing it. Do you eventually pick stuff like that up as you learn it?
Last edited on
Can I fix the code by calling the int in Line 18 a double?


float and double exhibit the same types of problems, just that with double the errors are smaller. That is, 2.0 as a double might be 2.0 +/- 3e-16, while as a float it could be 2.0 +/-3 e-7. But the problems are the same: neither are equal to 2.0.

I understand now why I get the error, but have no idea how to go about fixing it.


I explained how to fix it in my first post. Explicitly cast it with static_cast - that means you are purposely doing a cast, as opposed to allowing potential problems (with a warning) with an implicit cast (using the casting rules )by the compiler.

Do you eventually pick stuff like that up as you learn it?


Like anything, it is a matter of knowing how individual things work. Don't worry, FP numbers have caused all kinds of problems to many a coder over a long time. Have your teachers explained how the different types are stored in the computer? Have a read about it with a Google search.

OMG, thank you thank you thank you. I finally got it to work!!! I'm sorry if I asked too many questions and was bothersome, but like I said I really want to understand it, but it's an online course and I (obviously) do better if I can ask a million question in class.
I'm sorry if I asked too many questions and was bothersome, but like I said I really want to understand it, but it's an online course and I (obviously) do better if I can ask a million question in class.


It's OK, everyone has to start somewhere, asking & answering questions is the purpose of the forum :+) If you have more questions, keep em' rolling in, there are lots of very talented people here - vastly more than me :+)

You seem to have a good attitude, so best of luck - I am sure you will do well.
Topic archived. No new replies allowed.