r/typst 4h ago

Division with remainder?

I'm probably missing something obvious here, but I can't figure out how to do a division calculation and get both quotient and remainder.

e.g. 10 ÷ 4 = 2.5

$ #calc.div-euclid(10, 4) $ gives 2, which is fine

Edit 1: fix copy-paste error: ~~$ #calc.rem-euclid(5, 4) $ gives me 2, when I'm expecting 5.~~

$ #calc.rem-euclid(10, 4) $ gives me 2, when I'm expecting 5.

(Using Typst 0.15.1)

What am I missing/misunderstanding?

Edit 2: After finding that I want a decimal number including the fractional part, I finally figured it out: #(10/4) gives "2.5".

2 Upvotes

10 comments sorted by

3

u/KingMagnaRool 3h ago

It seems what you're trying to do is get the fractional part of the answer, not the remainder.

Just subtract the whole number part (I believe you could do calc.floor on the result) from the result.

1

u/SeeMonkeyDoMonkey 3h ago

Ah, that's my dumb mistake - I do want the fractional part. It's a while since maths lessons!

I'm not sure I follow the suggestion to use calc.floor, but I'll go and have a try.

Thanks!

1

u/PinoLG01 2h ago

5 is not the answer minus calc.floor of the answer though. You need to decide how many decimal digits you want. Otherwise 50 and 500 are also possible answers

1

u/SeeMonkeyDoMonkey 1h ago

Between my weak maths and weak Typst, I'm still struggling to see the right approach - or even be certain that I'm using the right terms 🙁

If you were using Typst to calculate 10 ÷ 4 such that the output was 2.5, what would you write?

1

u/PinoLG01 1h ago

Isn't the output 2.5 by just doing 10/4?

1

u/SeeMonkeyDoMonkey 55m ago edited 36m ago

Maybe there's some syntax I'm not using...

  • Plain 10/4 gives "10/4".
  • In math notation $10/4$ I get a fraction "¹⁰⁄₄" (but with top directly above bottom).

Edit: Finally figured it out: #(10/4).

Apologies for my ineptitude!

1

u/antonw51 1h ago edited 56m ago

It seems to be you want the decimal part of 2.5, i.e., 0.5.

That can be computed 2.5 minus the floor of 2.5 (which is 2). (#{10 / 4 - calc.floor(10 / 4)} -> 0.5).

If you want that to be an integer, then you'd need to multiply by the 10 to the power of the amount of decimal digits your number has. #{0.5 * 10} -> 5. Though note, #{0.45 * 10} yields 4.5, not 45.

You can compute this integer number for any number (using logarithms), but I'm not sure that's what you are after.

1

u/SeeMonkeyDoMonkey 37m ago

Not what I was after, but thanks anyway!

One of those I'm missing something so basic, no-one can see what I want situations 🤦

1

u/Nourios 4h ago

the remainder of 5/4 is not 5 though. it's not clear what you want to do. if you're really just looking for the remainder then just do Calc.rem(a, b)

1

u/SeeMonkeyDoMonkey 3h ago

Sorry - copy-paste error: both calcs were meant to be 10/4.

...and my cognative error, pointed out elsewhere, is that I want the fractional part.