__int64 convert to int

K

Kio

Hi,

For example:

1 __int64 fib4(__int64 n)
2 {
3 if ((n==1) || (n==2)) return 1;
4 else return (fib(n-1)+fib(n-2));
5 }

How to avoid conversion to int in line 4 ??

Thx Kio
 
D

Dietmar Kuehl

Kio said:
For example:

1 __int64 fib4(__int64 n)
2 {
3 if ((n==1) || (n==2)) return 1;
4 else return (fib(n-1)+fib(n-2));
5 }

How to avoid conversion to int in line 4 ??

Hard to tell: '__int64' is not a standard type and the rules set
up for the type are thus environment specific. You should ask this
question in a forum devoted to the specific environment you are
using.

BTW, it is a pretty bad idea to compute Fibonacci numbers using a
recursive approach in C++. The compiler will not optimize the call
to reuse results computed before as is usual for functional
programming languages (or even the C++ compiler when computing the
value using template meta programming techniques). A simple
iterative approach is much faster.
 
K

Kio

Hard to tell: '__int64' is not a standard type and the rules set
up for the type are thus environment specific. You should ask this
question in a forum devoted to the specific environment you are
using.

BTW, it is a pretty bad idea to compute Fibonacci numbers using a
recursive approach in C++. The compiler will not optimize the call
to reuse results computed before as is usual for functional
programming languages (or even the C++ compiler when computing the
value using template meta programming techniques). A simple
iterative approach is much faster.

I know :) I'm just testing ... Do you know maybe, which compiler would not
convert it ??
 
P

persenaama

I know :) I'm just testing ... Do you know maybe, which compiler would not
convert it ??

That's not in the C++ standard, so far I only seen that as
intrinsic/extension in the Microsoft Visual C++ .. I assume that's
where you using it, so most likely the answer is 'no'. I can write you
__int64 class which won't but what's the point? :)
 
M

Michiel.Salters

Dietmar said:
Hard to tell: '__int64' is not a standard type and the rules set
up for the type are thus environment specific. You should ask this
question in a forum devoted to the specific environment you are
using.

A fair guess would be

if ( __int64(0) < n && n < __int64(3) ) { /*...*/ }

which assumes an operator<(__int64,__int64) and a
__int64::__int64(int),
both of which seem reasonable. If you consider that
environment-specific,
just add template<typename T> and replace __int64 with T ;-)

Michiel.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top