Author Topic: modulo only works with integers?  (Read 2895 times)

I've just started coding about a day ago so bear with me.

I want to check if a number is whole or not so I used modulo to check this, like is most other languages. It just only works with integers though, if I do echo(5.4 % 1); it always prints 0.
So, is there any way I can get something working or is there any function I can use that will check if a number is an int/float?

Quote
In computing, the modulo operation finds the remainder after division of one number by another (sometimes called modulus).

There is no remainder when you divide by 1.

There are many ways you can check if a number is a whole number. Below are some of them.
if(%number $= mFloor(%number))
if(%number $= mFloatLength(%number, 0))
if(stripChars(%string, "-0123456789") $= "")

if(stripChars(%string, "-0123456789") $= "")
Won't work on large floats like 1.2345e+009

There are many ways you can check if a number is a whole number. Below are some of them.
if(%number $= mFloor(%number))
alright thanks.
Also, what's the difference between == and $= ? I've heard that they're used for strings but that doesn't seem to be the case here.

== is for comparing numbers, $= is for strings.

I recommend you just do
if(%number $= mFloor(%number))

But for anybody looking to a solution for the modulus problem:

Code: [Select]
function mMod(%num, %a) { //Supports floats unlike %
return %num - mFloor(%num / %a) * %a;
}

I believe this is the most efficient way of doing this (well Port or Trinick is gonna come along and do stuff with binary operators or weird functions nobody's ever heard of, but that's true for any function you post, so whatever).

You should use % for integers where possible though, because that's like 20x faster.
« Last Edit: February 13, 2015, 01:56:00 PM by boodals 2 »

== is for comparing numbers, $= is for strings.
I understand that but why is it used in this
if(%number $= mFloor(%number)) ?
Aren't those two numbers?

yeah the funny thing is that torque can p much handle anything as a string. the typing is way goofy

== would work as well there if %number is a number but if %number doesn't represent a number (say "e"), it would always yield true even though it shouldn't

basically the way it breaks down is

if("e" == mFloor("e"))
if(0 == mFloor(0))
if(0 == 0)
if(true)


for w/e reason any string where the first word isn't a non-zero number (except "true") evaluates to false, or 0 ("1234 lobster" would be seen as having a value of "1234" i'm p sure)

by using $= and handling the values as strings instead, you eliminate that issue

if("e" $= mFloor("e"))
if("e" $= mFloor(0))
if("e" $= 0)
if(false)


i'm pretty sure that's all right but there are some more details about goofy torque strings that aren't relevant

You can really use $= to compare anything in standard context. The difference becomes necessary doing stuff above my level.

Basically what otto-san beat me to.

snip
alright thanks. the way I'm doing it is that there is never going to be a string passed through, but should I use $= anyways? Is there any reason that I shouldn't be using $= ?

$= is what makes the check work, stick with it

the only reason i can think of to avoid strings (rather than bad practice in some cases maybe) is that it can be slower, but this is only really relevant on a much bigger scale

for w/e reason any string where the first word isn't a non-zero number (except "true") evaluates to false, or 0 ("1234 lobster" would be seen as having a value of "1234" i'm p sure)
i'm pretty sure that's all right but there are some more details about goofy torque strings that aren't relevant

Any string where the initial part is parsable as a number is parsed as that number; once TorqueScript hits any non-numeric character, it stops parsing the string as a number.

==>echo("1.2whydoesthiswork" + 3.7);
4.9

$= is what makes the check work, stick with it

the only reason i can think of to avoid strings (rather than bad practice in some cases maybe) is that it can be slower, but this is only really relevant on a much bigger scale
Ok thanks. I'll stick with $=.

Anyways, I've probably over-complicated what I'm trying to do, so I'll give you the context to see if there is an easier way of doing things:
Basically, I'm going to be getting a time every 100ms, counting up/down from numbers that I specify. The times that I will be getting are going to be 0, 100, 200, ... 2800, 2900, 3000, etc...
I want to display these times to the user as seconds. I've nailed this fine, and everything is working, but it annoys me when it goes from 2.8, 2.9 then 3, skipping 2 characters and making the text on the right jump back and fourth. So I just added a .0 to all whole numbers as they are getting displayed. this is the function I am using on every number in the commandToClient('bottomprint'...) line:

Code: [Select]
function milToDec(%num)
{
%num /= 1000;
if(%num $= mFloor(%num))
%num = %num @ ".0";
return %num;
}
is there a better way of adding a .0 to whole numbers?