Raised This Month: $ Target: $400
 0% 

Multiplications with float


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
condolent
AlliedModders Donor
Join Date: Jan 2016
Location: gc_sLocation;
Old 07-29-2017 , 20:02   Multiplications with float
Reply With Quote #1

Hi, when I try to make a multiplication with a float value it tells me there's a tag mismatch for some reason.

PHP Code:
int wonCredits betCredits 0.5
Why does it tell me that and what's the proper way of doing this??
__________________
condolent is offline
shavit
AlliedModders Donor
Join Date: Dec 2011
Location: Israel
Old 07-29-2017 , 20:22   Re: Multiplications with float
Reply With Quote #2

Edit: I was partially wrong, see Fyren's reply!

It depends on what you're trying to achieve.

It's better to round this value or just straight up cast it to int (use view_as<int>(betCredits * 0.5) for that).
Casting to integer will make it round to floor. So if you bet on 7 credits, you'll win 3 and not 3.5 or 4.
If you want to make it 3.5, change int to float (do not use view_as<type>()).

There's a tag mismatch because you're multiplying an int by a float, which is a different tag (type). You can achieve the same thing but avoid the tag mismatch warning by dividing by 2 (/ 2) instead of multiplying by 0.5.
__________________
retired

Last edited by shavit; 07-29-2017 at 22:31.
shavit is offline
condolent
AlliedModders Donor
Join Date: Jan 2016
Location: gc_sLocation;
Old 07-29-2017 , 20:25   Re: Multiplications with float
Reply With Quote #3

Quote:
Originally Posted by shavit View Post
It depends on what you're trying to achieve.

It's better to round this value or just straight up cast it to [tt]int[tt] (use view_as<int>(betCredits * 0.5) for that).
Casting to integer will make it round to floor. So if you bet on 7 credits, you'll win 3 and not 3.5 or 4.
If you want to make it 3.5, change int to float (do not use view_as<type>()).

There's a tag mismatch because you're multiplying an int by a float, which is a different tag (type). You can achieve the same thing but avoid the tag mismatch warning by dividing by 2 (/ 2) instead of multiplying by 0.5.
That's one hell of a good explanation!
Before I did this instead:
PHP Code:
float wonCredits betCredits 0.5
And then where it does something with wonCredits, I just made it RoundToNearest(wonCredits), it's untested but in theory it should work fine if I'm not mistaken
__________________
condolent is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 07-29-2017 , 22:23   Re: Multiplications with float
Reply With Quote #4

Quote:
Originally Posted by shavit View Post
It's better to round this value or just straight up cast it to [tt]int[tt] (use view_as<int>(betCredits * 0.5) for that). Casting to integer will make it round to floor.
view_as is not a C-style cast. This won't do what you're suggesting it does. You'll get back the integer representing the bits of the float.

Quote:
Originally Posted by shavit View Post
There's a tag mismatch because you're multiplying an int by a float, which is a different tag (type). You can achieve the same thing but avoid the tag mismatch warning by dividing by 2 (/ 2) instead of multiplying by 0.5.
Multiplying an int and a float does not result in a tag mismatch, assigning the result to an int does, because the result is a float. Doing an integer division by 2 isn't exactly the same thing since it'll truncate and give you an int back.
Fyren is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:03.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode