Serving the Quantitative Finance Community

 
User avatar
hungryquant
Topic Author
Posts: 0
Joined: May 21st, 2008, 2:26 pm

c++ int to float back to int conversion

December 2nd, 2009, 2:36 pm

hey guys I am wondering if it is possible to force integer to float type conversion to always take the next highest value. the c++ standard seems to be:"When an object of integral type is converted to a floating type and the original value cannot be represented exactly, the result is either the next higher or the next lower representable value."so for example if x is some integer number within range of both int and a float bounds. int i = xfloat f = i;Then covert back to an int int j = f;It might be possible that j could be x-1 (as float to int chops of anything to the right of the point)?What I basically would like to know is whether its possible to perform an int-float-int conversion and have the inputted integer one less thatn the outputted integer. And if so can I do anything about it ?
Last edited by hungryquant on December 1st, 2009, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 20253
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

c++ int to float back to int conversion

December 2nd, 2009, 3:43 pm

would ceil and floor functions in <cmath> or <math.h> not do the job? x = 2.6;ceil(x) == 3.0;floor(x) == 2.0;
Last edited by Cuchulainn on December 1st, 2009, 11:00 pm, edited 1 time in total.
 
User avatar
hungryquant
Topic Author
Posts: 0
Joined: May 21st, 2008, 2:26 pm

c++ int to float back to int conversion

December 2nd, 2009, 3:50 pm

Yeah that would sort that problem if it occurs Thanks.
 
User avatar
Costeanu
Posts: 1
Joined: December 29th, 2008, 5:33 pm

c++ int to float back to int conversion

December 2nd, 2009, 7:21 pm

I'm not sure I understand Cuch's solution, but here's another idea. Take the number 12345678901. If you convert to float you get 1.23457e+010, and when you convert back you get 12345678848, which is lower than the original value. In hexa the original number is 2dfdc1c35, and the second is 2dfdc1c00, i.e. rounds to the first 7 significant (hex) places. The algorithm you propose should be: - check if any hex digits after the first 7 are non-zero- and if so, increment by 1 the 7th digit (with carry, if necessary) and replace the digits to the right with 0. For negative numbers it's more complicated, I suppose you should first change the sign, do the similar manipulation but round down, and then change the sign again. Best,V.
 
User avatar
hungryquant
Topic Author
Posts: 0
Joined: May 21st, 2008, 2:26 pm

c++ int to float back to int conversion

December 3rd, 2009, 8:25 am

QuoteOriginally posted by: CosteanuI'm not sure I understand Cuch's solution, but here's another idea. Take the number 12345678901. If you convert to float you get 1.23457e+010, and when you convert back you get 12345678848, which is lower than the original value. In hexa the original number is 2dfdc1c35, and the second is 2dfdc1c00, i.e. rounds to the first 7 significant (hex) places. The algorithm you propose should be: - check if any hex digits after the first 7 are non-zero- and if so, increment by 1 the 7th digit (with carry, if necessary) and replace the digits to the right with 0. For negative numbers it's more complicated, I suppose you should first change the sign, do the similar manipulation but round down, and then change the sign again. Best,V.Thats very interesting. I think this is covering a second problem which is conversion in the case of the number being too large to represent preciesly as a float ? The numbers I would be dealing with would be of a size that this would not be such an issue (though its good to be aware of the potential issue)
 
User avatar
Costeanu
Posts: 1
Joined: December 29th, 2008, 5:33 pm

c++ int to float back to int conversion

December 3rd, 2009, 10:53 am

I'm not sure I understand. Only for large numbers you can get an int-float-int conversion that does not return the original number. Best,V.
 
User avatar
AVt
Posts: 90
Joined: December 29th, 2001, 8:23 pm

c++ int to float back to int conversion

December 3rd, 2009, 11:38 am

Not clear what you want and why ... do you mean an Integer or int? And float or floating point number?For C you check limits in limit.h for the data types and rounding modes (which may also depend on your compiler and settings), usually IEEE 754 round nearest/even and look up the specification for the mantissa for data type float (53 bit for double), may be that is what Costeanu already said.But C++ usually does not commit the IEEE standard if I remember correctly, so certainly you have to check the documentation for your compiler (and may be platform + OS).
 
User avatar
hungryquant
Topic Author
Posts: 0
Joined: May 21st, 2008, 2:26 pm

c++ int to float back to int conversion

December 3rd, 2009, 1:32 pm

QuoteOriginally posted by: CosteanuI'm not sure I understand. Only for large numbers you can get an int-float-int conversion that does not return the original number. Best,V.I read the following reagrsing int to float conversion (for c++)"When an object of integral type is converted to a floating type and the original value cannot be represented exactly, the result is either the next higher or the next lower representable value."This seems to me to indicate that its possible that converting an integer value to a float could lead to the integer value of the int being converted to a number just below, say for example 10 becomes 9.9999999. If this did happen and I then converted the float 9.9999999 to an int type it would cut the portion of the number down to 9 to be stored. Trying this example on my system (using gcc in a cygwin environment) on the following code#include<iostream>int main(){int i = 10;std::cout <<i<<"\n";float j = i;std::cout <<j<"\n";int k = j;std::cout <<k<<"\n";} gives 101010So its seems to be ok. What I was wondering is if it is at least a possibility that the int-float-int conversion could cause the outputted int to differ from the inputted one.Costeanu has given an example in which it would if the int is large. What i wan't to know is could this happen for smaller ones.If the answear is no then there is no problem, but if its not certain I would have to come up with a solution.
Last edited by hungryquant on December 2nd, 2009, 11:00 pm, edited 1 time in total.
 
User avatar
daveangel
Posts: 5
Joined: October 20th, 2003, 4:05 pm

c++ int to float back to int conversion

December 3rd, 2009, 1:45 pm

QuoteOriginally posted by: hungryquantQuoteOriginally posted by: CosteanuI'm not sure I understand. Only for large numbers you can get an int-float-int conversion that does not return the original number. Best,V.I read the following reagrsing int to float conversion (for c++)"When an object of integral type is converted to a floating type and the original value cannot be represented exactly, the result is either the next higher or the next lower representable value."This seems to me to indicate that its possible that converting an integer value to a float could lead to the integer value of the int being converted to a number just below, say for example 10 becomes 9.9999999. If this did happen and I then converted the float 9.9999999 to an int type it would cut the portion of the number down to 9 to be stored. Trying this example on my system (using gcc in a cygwin environment) on the following code#include<iostream>int main(){int i = 10;std::cout <<i<<"\n";float j = i;std::cout <<j<"\n";int k = j;std::cout <<k<<"\n";} gives 101010So its seems to be ok. What I was wondering is if it is at least a possibility that the int-float-int conversion could cause the outputted int to differ from the inputted one.Costeanu has given an example in which it would if the int is large. What i wan't to know is could this happen for smaller ones.If the answear is no then there is no problem, but if its not certain I would have to come up with a solution.the mantissa in a double will be big enough to hold ints. the mantissa is 52 bits and ints are 31 bits. so i don't think you will have an issue.Edited: changed 32 to 31 bits (I forgot the sign bit in an int).
Last edited by daveangel on December 3rd, 2009, 11:00 pm, edited 1 time in total.
knowledge comes, wisdom lingers
 
User avatar
hungryquant
Topic Author
Posts: 0
Joined: May 21st, 2008, 2:26 pm

c++ int to float back to int conversion

December 3rd, 2009, 1:53 pm

Thanks Dave Angel. I will check the GCC documentaion to see if it would use this method. There appears to be no problem with any of the integer values I have tried this code with, so I think your right.
Last edited by hungryquant on December 2nd, 2009, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 20253
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

c++ int to float back to int conversion

December 3rd, 2009, 3:30 pm

It feels like an accuracy/round off issue. Would boost Interval and/or boost Rational be useful here? in one case you can accomodate round off and in other there is no round off. But maybe that is not what you want. http://www.boost.org/doc/libs/1_41_0/li ... terval.htm And this boost library seems to be discussing round off policies. Maybe...
Last edited by Cuchulainn on December 2nd, 2009, 11:00 pm, edited 1 time in total.
 
User avatar
hungryquant
Topic Author
Posts: 0
Joined: May 21st, 2008, 2:26 pm

c++ int to float back to int conversion

December 3rd, 2009, 3:51 pm

thanks for the heads up cuch.i did come across this before. For what I'm doing if what dave Angel says is correct (and it make alot of sense) then it would be enough. This particular boost library would seem to be overkill for what I want.
 
User avatar
AVt
Posts: 90
Joined: December 29th, 2001, 8:23 pm

c++ int to float back to int conversion

December 3rd, 2009, 6:48 pm

Ok, INT_MAX = 2147483647 = 2^31 - 1 and FLT_MANT_DIG = 24 and beyond thatyou get your troubles (using VC++ 2005 here).#include<iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ double x; int n, k; n = INT_MAX; x = (float)(n); k = (int)x; cout << "n = " << n << ", x = " << x << ", k = " << k << endl << endl; n = 16777216 + 1; // 2^24 + 1 x = (float)(n); k = (int)x; cout << "n = " << n << ", x = " << x << ", k = " << k << endl << endl; n = 16777216 + 2; // 2^24 + 2 x = (float)(n); k = (int)x; cout << "n = " << n << ", x = " << x << ", k = " << k << endl << endl; n = 16777216 + 3; // 2^24 + 3 x = (float)(n); k = (int)x; cout << "n = " << n << ", x = " << x << ", k = " << k << endl << endl; system("PAUSE"); return 0;}givesn = 2147483647, x = 2.14748e+009, k = -2147483648n = 16777217, x = 1.67772e+007, k = 16777216n = 16777218, x = 1.67772e+007, k = 16777218n = 16777219, x = 1.67772e+007, k = 16777220For the rest you may wish to look up or search for rounding modes.
 
User avatar
hungryquant
Topic Author
Posts: 0
Joined: May 21st, 2008, 2:26 pm

c++ int to float back to int conversion

December 4th, 2009, 1:24 pm

QuoteOriginally posted by: AVtOk, INT_MAX = 2147483647 = 2^31 - 1 and FLT_MANT_DIG = 24 and beyond thatyou get your troubles (using VC++ 2005 here).#include<iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ double x; int n, k; n = INT_MAX; x = (float)(n); k = (int)x; cout << "n = " << n << ", x = " << x << ", k = " << k << endl << endl; n = 16777216 + 1; // 2^24 + 1 x = (float)(n); k = (int)x; cout << "n = " << n << ", x = " << x << ", k = " << k << endl << endl; n = 16777216 + 2; // 2^24 + 2 x = (float)(n); k = (int)x; cout << "n = " << n << ", x = " << x << ", k = " << k << endl << endl; n = 16777216 + 3; // 2^24 + 3 x = (float)(n); k = (int)x; cout << "n = " << n << ", x = " << x << ", k = " << k << endl << endl; system("PAUSE"); return 0;}givesn = 2147483647, x = 2.14748e+009, k = -2147483648n = 16777217, x = 1.67772e+007, k = 16777216n = 16777218, x = 1.67772e+007, k = 16777218n = 16777219, x = 1.67772e+007, k = 16777220For the rest you may wish to look up or search for rounding modes.Thanks AVT, the numbers I use are nowhere near that scale so it look like I am safe.Thank you to everyone who gave me help on this. I have learnt alot more about c++ variable storage through your responses. Chris