Skip to main content
GameDev.net gamedev.net
🔒 Locked

Silly error

Started by Sinner_Zero Dec 29, 2004 at 12:42 AM 12 replies 3k views
Original Post
Sinner_Zero
Sinner_Zero
Ok, I think this one will be simple for you guys to figure out, because it has me stumped, and... it's just really annoying. invalid operands of types float' and float' to binary `operator%' I get that for a line that looks pretty much liek this. float hello = 500.0f%343223.0f; So.... what I'm getting from this is that modulus doesn't work with floats... what? I know that's not right. Please tell me I'm doing something obviously wrong. Especially since if I cut out the .0f it works, which means int's is fine... so I just don't know what's going on anymore.
Morbo
Morbo
You can only modulous by an integer value. Something mod a float value doesn't make any sense. But you can do float%int.

What are you trying to do, exactly?
Sinner_Zero
Sinner_Zero
*thinks hard*
Ok maybe I need more sleep...

Why wouldn't modding by a floar make sense? you still get some value for a remainder through division of two floats... explain please?
JanT
JanT
Quote:
Original post by Morbo
Something mod a float value doesn't make any sense.


But of course it does. That's why there's fmod: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_fmod.asp
Sinner_Zero
Sinner_Zero
Ok, but normal C/C++ % doesn't do %(float,int) then right?
Sinner_Zero
Sinner_Zero
invalid operands of types float' and int' to binary `operator%'

float hello = 500.0f%3242;

...

what's going on...
Sinner_Zero
Sinner_Zero
Firstly the original response said you can't take float%float but you can float%int.

Secondly, thank you for the fmod, I was trying to stay away from non standard headers/libraries, then I saw it was part of math, thank you.

As for why... umm... first off a typecast to int destroys any part of the float you'd usually want... so why I need a float when I'm using floats... not the best question.

5.24567356 % 4.2736495 = .97202406
All your examples were w/ typecasting of course, but I explained above why that is no good.

Thanks for the fmod JanT.
Sinner_Zero
Sinner_Zero
"although casting your floats to ints would give you the same results."

Oh, there it is, that's what's really wrong. Sorry... I just had to point it out because that was my issue with that reply.

But thanks for the help peeps, IRC is blocked here... or our external IP is banned, I forget.
MaulingMonkey
MaulingMonkey
Quote:
Original post by Anonymous Poster
5 % 4 = 1
(int)5.24567356 % (int)4.2736495 = 1 // what is it that you want to get from a two float mod that you can't get with an int mod?


~_~

5 % 1 = 0
5.1 % 1.9 = 1.3

I rest my case. Basic implementation (err, not sure if it would work w/ negative values):

float fmod( float value , float divisor ){    return value - divisor * static_cast< int >( value / divisor );}
Sinner_Zero
Sinner_Zero
LOL

Nice proof.

Nice implementation. But we're trying to stick to standards here =p

Although... it might look cooler if it's my own fmod.

It also reminded me that I kindof have another question... although I think it's just the compiler being funny.

float hello = static_cast(var)%int;

Was saying vartype%int error instead of float%int error. Any ideas why? even though I casted it?

Or is it probably just a glitch (kinda).
Sinner_Zero
Sinner_Zero
Mauling, your implementation doesn't work for negative numbers...

Let's play, who can fix it the prettiest way (I won't actaully use it but I've been out of class for 2 months almost and this is fun).

Oh WAIT! I'm wrong. So nevermind, it does work with negatives, silly me, I forgot the basic rules of ... math?

DUN DUN DUN.

I'm hungry... bye.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.