# gcd() and lcm() ... need '%'

#### vefatica

Of all @EVAL's functions only (?) gcd() and lcm() require '%' on a variable name.
Code:
``````v:\> set num=21

v:\> echo %@eval[gcd(num 18)]
TCC: Syntax error "gcd(num 18)"

v:\> echo %@eval[lcm(num 18)]
TCC: Syntax error "lcm(num 18)"

v:\> echo %@eval[gcd(%num 18)]
3

v:\> echo %@eval[lcm(%num 18)]
126``````

#### rconn

gcd and lcm aren't functions, they're operators.

#### vefatica

gcd and lcm aren't functions, they're operators.
Mea culpa! They don't act like the other operators.

#### rconn

They're not floating point / bcd, they're 64-bit integer operations (like the rotate operators).

#### vefatica

They're not floating point / bcd, they're 64-bit integer operations (like the rotate operators).
Why? Arent they built into MAPM?

#### rconn

Why? Arent they built into MAPM?

No, they're not. (Neither are the rotate operators.) MAPM is for floating point operations, not integer-only.

#### vefatica

I think Gnu's version has CGD and LCM.

They're pretty easy to do anyway (and fast). This works in principle, but is subject to the limitations of @MAX and @MIN. There are other ways to figure out which is bigger.
Code:
``````::GCD.BTM
set dividend=%@MAX[%1,%2]
set divisor=%@MIN[%1,%2]
do forever
set remainder=%@EVAL[dividend MOD divisor]
if %remainder == 0 leave
set dividend=%divisor
set divisor=%remainder
enddo
echo GCD = %divisor``````

And

LCM(x,y)=x * y / GCD(x,y)

#### vefatica

Are you sure your MASM doesn't have GCD and LCM? I have an old version that includes
Code:
`````` *  M_APM  -  mapm_gcd.c
*
*  Copyright (C) 2001 - 2007   Michael C. Ring``````
There's a version on GitHub that also has that file. https://github.com/LuaDist/mapm

In fact, I just built it for the first time (even though I have no idea how to use it). I did it with VC8 using a batch file written for VC6.

#### vefatica

I built it again with VC10 (using the same BAT file). And with its CALC.EXE (which was also built) I get:
Code:
``````p:\mapm> calc 6 15 GCD
3.0

p:\mapm> calc 6 15 LCM
30.0``````

#### JohnQSmith

with its CALC.EXE (which was also built)
I built CALC before also. It's my go-to command line calculator. RPN FTW.

#### vefatica

I built CALC before also. It's my go-to command line calculator. RPN FTW.
CALC reminds me of when hand-held calculators first hit the scene. HPs used reverse polish notation, which I was never (and still am not) any good at.

My goto at the command line is a plugin command, EVAL, which is just a wrapper for @EVAL. You can do the same with an alias.

Replies
5
Views
1K
Replies
3
Views
624
Replies
5
Views
994
Replies
0
Views
711
Replies
1
Views
912
Replies
2
Views
1K
Replies
5
Views
2K
Replies
4
Views
982
Replies
1
Views
2K
Replies
10
Views
2K
Replies
3
Views
2K
Replies
3
Views
2K
Replies
8
Views
2K
Replies
13
Views
4K
Replies
6
Views
2K
Replies
3
Views
2K
Replies
1
Views
2K
Replies
4
Views
3K
Replies
5
Views
3K
Replies
0
Views
3K
Replies
1
Views
2K
B
Replies
54
Views
26K
Replies
8
Views
5K