# Doing math with TCC

#### pb4072

Hello,
Can someone tell me how to do simple math with TCC? I need to divide the height versus the width of PDF files, which I get from a "pdfinfo" utility. I need that ratio so that I can use it to make SVG files from the PDFs, at particular sizes.

Thanks,
Peter

#### Charles Dye

##### Super Moderator
One way is to use the %@EVAL function:
Code:
``set aspect=%@eval[%height / %width]``

Or you can use SET /A, which works much like in CMD.EXE but without the octal support biting you at odd moments:
Code:
``set /a aspect=%height / %width``

#### pb4072

Thanks.
One way is to use the %@EVAL function:
Code:
``set aspect=%@eval[%height / %width]``

Or you can use SET /A, which works much like in CMD.EXE but without the octal support biting you at odd moments:
Code:
``set /a aspect=%height / %width``

Thanks. Yeh, I've tried the "set /a" stuff in the command shell, but, it doesn't support floating numbers. I'll play with the eval thing. Thanks again!

#### vefatica

CMD may not, but TCC's SET /A supports floats.
Code:
``````v:\> set /a float=8/3
2.6666666667

v:\> set /a float*=.5
1.3333333334``````
@EVAL just works better.
Code:
``````v:\> echo %@eval[8/3 * .5]
1.3333333333``````

#### pb4072

CMD may not, but TCC's SET /A supports floats.
Code:
``````v:\> set /a float=8/3
2.6666666667

v:\> set /a float*=.5
1.3333333334``````
@EVAL just works better.
Code:
``````v:\> echo %@eval[8/3 * .5]
1.3333333333``````
CMD may not, but TCC's SET /A supports floats.
Code:
``````v:\> set /a float=8/3
2.6666666667

v:\> set /a float*=.5
1.3333333334``````
@EVAL just works better.
Code:
``````v:\> echo %@eval[8/3 * .5]
1.3333333333``````

Thanks. Yeh, I got %@eval to work for me.
Cheers.

#### Charles Dye

##### Super Moderator
CMD may not, but TCC's SET /A supports floats.
Code:
``````v:\> set /a float=8/3
2.6666666667

v:\> set /a float*=.5
1.3333333334``````
@EVAL just works better.
Code:
``````v:\> echo %@eval[8/3 * .5]
1.3333333333``````

Is it really any different?
Code:
``````C:\>set /a float=8 / 3 * .5
1.3333333333

C:\>``````

It's probably the same engine under the hood. Rex isn't one to write two different routines to accomplish the same task.

#### Charles Dye

##### Super Moderator
Thanks. Yeh, I've tried the "set /a" stuff in the command shell, but, it doesn't support floating numbers.

Either I'd forgotten that, or I never knew it in the first place. In TCC, it does support floating point.

#### WavSlave

pb4072, @EVAL lets you do some pretty advanced math as well. It supports exponentiation, bit-manipulation operations, logarithms, trigonometric functions, etc., etc. Want to know what pi is out to 15,000 decimal places? It's got that covered. It's really quite a powerful function should you ever need any of those facilities.

If I have any (very minor) gripe about it, it's that it has no particularly straightforward way to compute roots but I figured out how to get it to do those, too. You can do that by raising a number to the power of the reciprocal of the root you're trying to find. E.g., to find the cube root of n, use @EVAL[n**(1/3)].

#### vefatica

E.g., to find the cube root of n, use @EVAL[n**(1/3)].
That's about as straightforward as it gets!

#### samintz

##### Scott Mintz
pb4072, @EVAL lets you do some pretty advanced math as well. It supports exponentiation, bit-manipulation operations, logarithms, trigonometric functions, etc., etc. Want to know what pi is out to 15,000 decimal places? It's got that covered. It's really quite a powerful function should you ever need any of those facilities.

If I have any (very minor) gripe about it, it's that it has no particularly straightforward way to compute roots but I figured out how to get it to do those, too. You can do that by raising a number to the power of the reciprocal of the root you're trying to find. E.g., to find the cube root of n, use @EVAL[n**(1/3)].

You *could* create functions to do that if you need it often.
Code:
``````function SQR=`%@eval[%1 ** 0.5]`
function ROOTn=`%@eval[%1 ** (1/%2)]`
function Root=`%@eval[10**(log10(%1)/%2)]`

echo Square root of 2=%@SQR[2],^ncubed root of 2=%@ROOTn[2,3]^nanother cubed root=%@Root[2,3]

echo %@eval[ 2 * %@sqr[2]]``````

#### vefatica

You don't even need to call %EVAL if you know you'll be in @EVAL anyway.
Code:
``````v:\> function root `(%1**(1/%2))`

v:\> echo %@root[8,3]
(8**(1/3))

v:\> echo %@eval[%@root[8,3]]
2``````

#### WavSlave

That's about as straightforward as it gets!
...says the mathematics professor.

Replies
5
Views
1K
Replies
1
Views
1K
Replies
3
Views
1K
Replies
10
Views
2K
Replies
1
Views
1K
Replies
11
Views
2K
Replies
12
Views
2K
Replies
4
Views
1K
Replies
2
Views
869
Replies
3
Views
1K
Replies
7
Views
7K
Replies
17
Views
2K
Replies
6
Views
2K
Replies
12
Views
1K
Replies
1
Views
106
Replies
1
Views
106
Replies
2
Views
182
Replies
5
Views
391
Replies
2
Views
202
Replies
11
Views
376
Replies
16
Views
243
Replies
2
Views
214
Replies
2
Views
164
Replies
0
Views
173
Replies
1
Views
183
Replies
6
Views
419
Replies
6
Views
350
Replies
0
Views
315
Replies
2
Views
264
Replies
6
Views
542
Replies
0
Views
275
Replies
0
Views
596
Replies
5
Views
448
Replies
0
Views
330
Replies
3
Views
553
Replies
3
Views
451
Replies
5
Views
1K
Replies
0
Views
442
Replies
3
Views
476
Replies
0
Views
322
Replies
0
Views
321
Replies
7
Views
555
Replies
2
Views
379
Replies
0
Views
359
Replies
9
Views
1K
Replies
3
Views
405
Replies
2
Views
543
Replies
4
Views
717
Replies
5
Views
793
Replies
6
Views
684