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 [B]int[/B] 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 [B]int[/B] type)
} Short answer: you can’t use the % operator on floating types — it’s an integer remainder operator — so either compute a floating-point remainder (use std::fmod) or convert the values to an integer type and take the integer remainder. Be explicit about which behavior you want because converting a float to an integer discards the fractional part (truncates toward zero) and converting values outside the integer’s range is undefined, so check ranges first. (cppreference.com)
If what you wanted was “remainder for floats” (not an integer remainder), use std::fmod from <cmath>. Example usage:
#include <cmath>
double a = 5.5, b = 2.0;
double r = std::fmod(a, b); // r == 1.5 std::fmod is the standard way to get the floating-point remainder and lives in <cmath>. (en.cppreference.com)
A couple of important gotchas to complement the answers above: casting then using % (as , and suggested) will truncate toward zero, so negative values behave differently than you might expect; std::fmod returns a remainder with the same sign as the dividend, while std::remainder follows the IEEE rule (nearest integral quotient) — pick the function whose sign/rounding semantics match your need. (saco-evaluator.org.za)
Practical recommendations: prefer local variables over globals for thread-safety and clarity; avoid decrement loops like suggested (they’re slow and susceptible to FP rounding); use explicit C++ casts (static_cast) when converting to make intent obvious; always check for divisor == 0, NaN/Inf, and range before converting floats to ints. Using static_cast documents intent and is the C++-style cast to prefer over C-style casts. (en.cppreference.com)
Jump to Post— Dani 5,664Also, 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.
Jump to Post— Dave Sinkula 2,398Hello 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 [b]int[/b] type) }The function returns …
Jump to Post— Natso 0something 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.
Im using the function in a switch statement and the other functions return float numbers.
I guess I could use local variables for that function, could I not?
sorry about that.
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.
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 [b]int[/b] type) }
The function returns the remainder of floating point division.
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.
Hello guys,
is there a way to declare global variables as [B]float[/B] and then convert them to [B]int[/B] 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
Hello
you can use a most c++ powerful operator
int a;
float numq = 60.7;
a = static_cast<int> (num1);
i don't now why poeple here don't use a c++ feature the are using a c-style
...
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:
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
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
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.