int(a) vs (int)a

G

Gernot Frisch

Hmm...

sometime I see code like:
int a = int(13.5+17.2);

I use to write:
int a = (int)(13.5+17.2);

is there any difference?



--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
 
G

Gianni Mariani

Gernot said:
Hmm...

sometime I see code like:
int a = int(13.5+17.2);

I use to write:
int a = (int)(13.5+17.2);

is there any difference?

Essentially no difference.

int( x ) - is the construction syntax.

(int)( x ) - is the (C like) type-cast syntax which should probably give
way to the "static_cast<int>( x )" syntax.
 
R

Robert ^^

Hmm...
sometime I see code like:
int a = int(13.5+17.2);

In this case, you take the integer part of your addition so in this case :
13,5 + 17,2 = 30,7, the result can be 31 or 30.
I use to write:
int a = (int)(13.5+17.2);

But in this case, you modify what you have in the parenthèse. When you have
text in the => ( ), and (int) in front of, it transform your text in numeric
with the ASCII table. So in my opinion there are no reasons to do
(int)(13.5+17.2) when you already have number in the => ( ).

I hope my explications are understandable. See you later ;)
 
J

Jack Klein

In this case, you take the integer part of your addition so in this case :
13,5 + 17,2 = 30,7, the result can be 31 or 30.

Do you know anything about conversions in C++? The result cannot be
anything other than 30 when 30.7 is converted to any integer type.
But in this case, you modify what you have in the parenthèse. When you have
text in the => ( ), and (int) in front of, it transform your text in numeric
with the ASCII table. So in my opinion there are no reasons to do
(int)(13.5+17.2) when you already have number in the => ( ).

Do you know anything about operators and casts in C++? This
expression adds the two double values 12.5 and 17.2 to produce the
double value 30.7. The cast outside the parentheses has absolutely no
effect on the evaluation of the expression inside them. The cast
causes this double value to be converted to int, dropping the fraction
and again the result must be 30.
I hope my explications are understandable. See you later ;)

I suggest you learn a little more about C++ before answering questions
here.
 
J

Jack Klein

Hmm...

sometime I see code like:
int a = int(13.5+17.2);

This is C++ initializer syntax, meant to resemble the invocation of a
parameterized constructor for a user defined type. It is illegal in
C.
I use to write:
int a = (int)(13.5+17.2);

This is a plain old ordinary C cast, and it is legal in C. If
portability to C makes any difference to you.
is there any difference?

Other than the C portability for built-in types, none.

And this is identical to both of them:

int a = 13.5+17.2;

Casting the conversion of a floating point value to an integer type in
C++ (or C), is a waste of typing. It buys you exactly nothing. The
conversion is automatic without the case, and if the integral part of
the floating point value is outside the range of the integer type, the
cast does not make the behavior any less undefined.
 
G

Gernot Frisch

Robert ^^ said:
In this case, you take the integer part of your addition so in this
case :
13,5 + 17,2 = 30,7, the result can be 31 or 30.

Mumpitz! Conversion to integer always drops the fraction part:
ASSERT(int(3.999999999) == 4);
I hope my explications are understandable. See you later ;)
Me, too.
 
G

Gernot Frisch

Casting the conversion of a floating point value to an integer type
in
C++ (or C), is a waste of typing. It buys you exactly nothing. The
conversion is automatic without the case, and if the integral part
of
the floating point value is outside the range of the integer type,
the
cast does not make the behavior any less undefined.

It drops some compiler warnings ;)
 
R

Robert ^^

Hmm...
Do you know anything about conversions in C++? The result cannot be
anything other than 30 when 30.7 is converted to any integer type.


Do you know anything about operators and casts in C++? This
expression adds the two double values 12.5 and 17.2 to produce the
double value 30.7. The cast outside the parentheses has absolutely no
effect on the evaluation of the expression inside them. The cast
causes this double value to be converted to int, dropping the fraction
and again the result must be 30.


I suggest you learn a little more about C++ before answering questions
here.

Sorry if i'm just a studient and if i want to help someone, you help nobody
when you say things like these ... But excuse me, i can't speak with you,
you are too stronger for me ...
 
K

Karl Heinz Buchegger

Robert said:
Sorry if i'm just a studient and if i want to help someone, you help nobody
when you say things like these ...

The problem is, that with answers like the one you gave, you also help nobody.
In fact the opposite is true: The group needs to take actions to correct your
errors (which by the way are silly and very basic errors).
 
R

Ron Natalie

Gianni said:
int( x ) - is the construction syntax.

(int)( x ) - is the (C like) type-cast syntax which should probably give
way to the "static_cast<int>( x )" syntax.

They are both called "explicit conversion" in the C++ standard.
The first is "functional notation" the second is "cast notation."

In the case where there is only one arg in the functional notation
form, the language defines the two as being identical.
 
R

Robert ^^

The problem is, that with answers like the one you gave, you also help
nobody.
In fact the opposite is true: The group needs to take actions to correct your
errors (which by the way are silly and very basic errors).

Sorry ... : S
 
K

Karl Heinz Buchegger

Robert said:
Sorry ... : S

No problem.
We all make errors from time to time (even the regulars).

Don't get us wrong. Nobody wants you to leave this group. You are very
welcome to this group.
But please read this group a while and stick to answering posts for which
you are very, very sure you know what you are talking about. E.g. I consider
myself to have a reasonable understanding of the language C++. Yet you won't
find replies with my name to the topic of 'templates'. They are one of
my weak points. I know it, so I refrain from posting.
 
D

Dave Vandervies

Hmm...

sometime I see code like:
int a = int(13.5+17.2);

I use to write:
int a = (int)(13.5+17.2);

is there any difference?

If you use the second form, you can compile your C++ with a C compiler.

This makes it Obviously Superior; just ask the people who keep trying
to tell comp.lang.c that we should be careful to write our C so that we
can compile it with a C++ compiler.


dave
 
P

Peter Julian

Gernot Frisch said:
Hmm...

sometime I see code like:
int a = int(13.5+17.2);

I use to write:
int a = (int)(13.5+17.2);

is there any difference?

You've already received some excellent comments about the explicit
conversions above. What i'ld like to point out is that in C++ the latter
statement might better be stated as so:

int a = static_cast<int>(13.5 + 17.2);

which denotes a compile-time conversion (static) and removes any confusion
about the intentions of the syntax.

Other interesting cast operators you may want to investigate include the
following:
dynamic_cast, const_cast and reinterpret_cast
 
R

Ron Natalie

Peter said:
int a = static_cast<int>(13.5 + 17.2);

which denotes a compile-time conversion (static) and removes any confusion
about the intentions of the syntax.
It's not a compile time conversion any more than the other.
static_cast<int>(13.5 + 17.2)
int(13.5+17.2)
(int)(13.5+17.2)
all have exactly the same meaning.

There's no requirement in the standard to evaluate double typed
constrant expressions at compile time.
 
P

Peter Julian

Ron Natalie said:
It's not a compile time conversion any more than the other.
static_cast<int>(13.5 + 17.2)
int(13.5+17.2)
(int)(13.5+17.2)
all have exactly the same meaning.

Yes, of course. I was refering to coding style only.
There's no requirement in the standard to evaluate double typed
constrant expressions at compile time.

That i didn't know.
 
D

davidrubin

Gernot said:
Hmm...

sometime I see code like:
int a = int(13.5+17.2);

I use to write:
int a = (int)(13.5+17.2);

is there any difference?

As far as I understand, the first form, "int a = int(b)", creates a
temporary, while the second does a compile-time conversion. /david
 
R

Ron Natalie

As far as I understand, the first form, "int a = int(b)", creates a
temporary, while the second does a compile-time conversion. /david
You don't understand. The language specifically requires both of them
to have the same behavior.

Both result in a temporary (which may be elided).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top