954,135 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

conversion type

Hello guys,
is there a way to declare global variables as float and then convert them to int to be used in a function? i.e.

float num1, num2, result;
float Mod(){
      num1%num2=result; //Wrong, (%) requires <strong>int</strong> type)
}
MaxC
Newbie Poster
11 posts since May 2004
Reputation Points: 12
Solved Threads: 0
 

Im using the function in a switch statement and the other functions return float numbers.

MaxC
Newbie Poster
11 posts since May 2004
Reputation Points: 12
Solved Threads: 0
 

I guess I could use local variables for that function, could I not?
sorry about that.

MaxC
Newbie Poster
11 posts since May 2004
Reputation Points: 12
Solved Threads: 0
 

Also, keep in mind that variables can be typecasted. For example:

int x = 5;
float sum = (float)(x) + 2.25;


I think I did that right, anyways.

cscgal
The Queen of DaniWeb
Administrator
19,421 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 

Hello guys, is there a way to declare global variables as float and then convert them to int to be used in a function? i.e.

float num1, num2, result;
float Mod(){
num1%num2=result; //Wrong, (%) requires <strong>int</strong> type)
}

The fmod function returns the remainder of floating point division.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

something like this should work:

myfloat <- float
myint <- int

myint = 0;
while(myfloat >= 1){
myint++;
myfloat--;
}

Something like that would do the trick... I might have my reasoning a little of but I'm sure that it would work.

Natso
Junior Poster in Training
51 posts since May 2004
Reputation Points: 10
Solved Threads: 1
 
Hello guys, is there a way to declare global variables as float and then convert them to int to be used in a function? i.e.

yea,sure thing use type casting.

Eg:

float x = 9.879;
int y = (int)x; //y will be 9

you can do it the other way around too.
x=(float)y;


Usually float and int can be assigned to each other as C++ normally does that type casting automatically

FireNet
Posting Whiz in Training
258 posts since May 2004
Reputation Points: 108
Solved Threads: 7
 

Hello
you can use a most c++ powerful operator
int a;
float numq = 60.7;
a = static_cast (num1);

i don't now why poeple here don't use a c++ feature the are using a c-style

abu_sager
Newbie Poster
10 posts since Jun 2004
Reputation Points: 12
Solved Threads: 2
 
... i don't now why poeple here don't use a c++ feature the are using a c-style


I think people don't often realize there's more than one kind of cast in C++. If that's news to anyone, go look up casting in a good C++ reference and find out what they are and how they work.

Here's a quick article on why to use C++ casting: http://www.sjbrown.co.uk/static_cast.html

For this problem, though, a C-style cast isn't very dangerous since we're only dealing with numeric types--the really bad stuff happens when you're casting pointers to classes.

--sg

gusano79
Posting Pro
519 posts since May 2004
Reputation Points: 182
Solved Threads: 77
 

two approaches that i can think of:

(1)
float num1, num2, result;
float Mod()
{
float result;
result = (float) ( (int) num1 % (int) num2 );
return result;
}

(2)
use fmod()
check the use of it on mdsn.microsoft.com

ivosetyadi
Newbie Poster
1 post since Jun 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You