int 2 float ??
how do i convert an int value to a float value ?
i like a strlen() value to be float
--------------
--- Ryball ---
--------------
----------------- Ryball -----------------
Thx :-)
--------------
--- Ryball ---
--------------
--------------
--- Ryball ---
--------------
----------------- Ryball -----------------
Ok, assuming you have an integer i and a float f, then the following are true.
The line "f = i;" will do the cast for you, because in C all values are automatically cast into higher types using the following rule:
char -> short -> int -> long -> float -> double
meaning if the type you have is left of the type you want to assign into, it will work correctly with no cast. I'm not sure if the char variable still casts up this way in C++.
Now, in order to remove those pesky wanrings (jk) you should usually us a cast yourself, and sometimes you need to anyway. So here are the general rules:
In C - this is a cast - using C cast syntax
(float) i
In C++ - this is the same cast - using contructor syntax
float(i)
and in C++ this is the cast using C++ cast operators
NOTE: replace the curly braces above with angle brackets
you can see that NeHe chose the second format, so he is using C++ and avoiding the more lengthly and less well known cast operators.
the cast operators are:
static_cast - for casting an object into a different type using a cast operator. This is the cast that replaces nearly all non-pointer casts from C.
dynamic_cast - for casting a polymorphic pointer into a decendent type.
const_cast - for changing the constness of an object (adding / removing the const qualifier).
reinterpret_cast - platform dependent, but usually necessary for casting non-polymorphic pointers to other types. 90% of the time reinterpret_cast means ... GENERATE NO CAST CODE, just treat this variable as if it WERE the other type. This is a lot like using a union in C.
well ... good luck.
Edited by - Xai on September 16, 2000 4:09:49 PM
Edited by - Xai on September 16, 2000 4:10:36 PM
Edited by - Xai on September 16, 2000 4:12:11 PM
The line "f = i;" will do the cast for you, because in C all values are automatically cast into higher types using the following rule:
char -> short -> int -> long -> float -> double
meaning if the type you have is left of the type you want to assign into, it will work correctly with no cast. I'm not sure if the char variable still casts up this way in C++.
Now, in order to remove those pesky wanrings (jk) you should usually us a cast yourself, and sometimes you need to anyway. So here are the general rules:
In C - this is a cast - using C cast syntax
(float) i
In C++ - this is the same cast - using contructor syntax
float(i)
and in C++ this is the cast using C++ cast operators
static_cast{float}(i)
NOTE: replace the curly braces above with angle brackets
you can see that NeHe chose the second format, so he is using C++ and avoiding the more lengthly and less well known cast operators.
the cast operators are:
static_cast - for casting an object into a different type using a cast operator. This is the cast that replaces nearly all non-pointer casts from C.
dynamic_cast - for casting a polymorphic pointer into a decendent type.
const_cast - for changing the constness of an object (adding / removing the const qualifier).
reinterpret_cast - platform dependent, but usually necessary for casting non-polymorphic pointers to other types. 90% of the time reinterpret_cast means ... GENERATE NO CAST CODE, just treat this variable as if it WERE the other type. This is a lot like using a union in C.
well ... good luck.
Edited by - Xai on September 16, 2000 4:09:49 PM
Edited by - Xai on September 16, 2000 4:10:36 PM
Edited by - Xai on September 16, 2000 4:12:11 PM
All those edits are because i don''t know how to use a source block, and therefore couldn''t make it display the angle bracket character. Sorry. If you know how, please tell me.
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement