Difference? (int)50.f and int(50.f) ?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Difference? (int)50.f and int(50.f) ?

Post by MasterGod »

What's the difference between (int)50.f and int(50.f).
I know the first one is casting but what's the second one?

P.S
Both giving me 50.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

I would say that in second case your compiler is quietly making cast for you in which case its the same as (int)(50.f)
killthesand
Posts: 42
Joined: Sat Sep 29, 2007 3:33 am
Contact:

Post by killthesand »

If I'm not mistaken, the first one is an explicit cast and the second one is directly calling the int constructor and returning an int. I think it's up to your compiler how the explicit cast actually works. Casting float to int shouldn't really matter. I found this example online which demonstrates a problem with explicit casts.

Code: Select all

// class type-casting
#include <iostream>
using namespace std;

class CDummy {
    float i,j;
};

class CAddition {
	int x,y;
  public:
	CAddition (int a, int b) { x=a; y=b; }
	int result() { return x+y;}
};

int main () {
  CDummy d;
  CAddition * padd;
  padd = (CAddition*) &d;
  cout << padd->result();
  return 0;
}
That code should compile without errors but it will crash when the pointer can't resolve the address of result()

If there was another constructor: CAddition(CDummy*); Then using the line padd = new CAddition(&d); should fix it. Calling a constructor should ensure a valid type.
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

So you say using the constructor would be safer at most of the cases? Cool, didn't knew that.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Trefall
Posts: 45
Joined: Tue Dec 05, 2006 8:49 pm

Post by Trefall »

If you want safe casting, I'd rather use the C++ standard casts than the C standard.

float f = 10.0f;
int i = static_cast<int>(f);

const char* a = "blabla";
char* b = const_cast<char*>(a);

float f = 10.0f;
unsigned char* uc = reinterpret_cast<unsigned char*>(&f);

class Base {...};
class Derived : public Base {...};
Base* b = new Base();
Derived* d = dynamic_cast<Derived*>(b);

Using the C++ standard allows the compiler to do type-safety checks, it's clearer than C style casts as to what the programmer intends to do, and C style casts can't do dynamic casts either.

For a more in-depth, but quick, read:
http://www.informit.com/guides/content. ... seqNum=134
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Re: Difference? (int)50.f and int(50.f) ?

Post by Acki »

MasterGod wrote:What's the difference between (int)50.f and int(50.f).
I know the first one is casting but what's the second one?

P.S
Both giving me 50.
maybe there is a difference between memory handling, as far as int(50.f) seems to create a new integer while (int)50.f only casts it ???

in either case for a single value it makes no difference for the result, but it does when you have a calculation !!!

Code: Select all

  printf("%d\n", 50.5 + 50.5);
  // output is 0, because we pass a floats result as integer (%d)
  // so we have to cast the value passed to the string
  // (yeah, I know it would be simpler to change to %f)

  // now we can cast the values...
  printf("%d\n", (int)50.5 + (int)50.5);
  // but the output is 100 !!!

  // or we can cast the result...
  printf("%d\n", int(50.5 + 50.5));
  // now the output is correct: 101
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Re: Difference? (int)50.f and int(50.f) ?

Post by vitek »

MasterGod wrote:What's the difference between (int)50.f and int(50.f).
I know the first one is casting but what's the second one?
They are equivalent. The first is a standard C style cast, and the second one is called a function style cast. You would typically avoid a function style cast if the type you were casting to is made up of multiple tokens. For instance casting to unsigned long or something like that.
killthesand wrote:I found this example online which demonstrates a problem with explicit casts.
This isn't a problem with explicit casts. That is a problem with any cast, especially pointer casts. If you cast something that shouldn't be you'll get bad results.
Acki wrote:maybe there is a difference between memory handling, as far as int(50.f) seems to create a new integer while (int)50.f only casts it ???
No. Both expressions result in a temporary int being created from the float.
Acki wrote:in either case for a single value it makes no difference for the result, but it does when you have a calculation !!!
This has nothing to do with the type of cast you use. It has everything to do with order of operations and truncation of float.

Code: Select all

// 50.5 + 50.5 = 101.0, then truncate to integer
const int a = int (50.5f + 50.5f);

// 50.5 truncated to integer becomes 50.
// 50 + 50 = 100
const int b = int (50.5f) + int (50.5f);
Travis
Post Reply