From 15cb5ccbbee7abb8ce253632bc2c4febdfaedca5 Mon Sep 17 00:00:00 2001 From: Julian Weigt Date: Mon, 9 Feb 2026 18:51:46 +0100 Subject: Add more comments to code. --- ratio.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'ratio.c') diff --git a/ratio.c b/ratio.c index 6d0b59d..f8bfeb0 100644 --- a/ratio.c +++ b/ratio.c @@ -9,6 +9,7 @@ typedef unsigned long long num; typedef struct {bool s; num n; num d;} rational; +/*safe_sum and safe_product abort if computations would exceed maximal value allowd for unsigned long long and overflow.*/ num safe_sum(num n1, num n2){ if(n2 > ULLONG_MAX-n1){ printf("Sum overflow: Adding %llu and %llu\n",n1,n2); @@ -107,6 +108,7 @@ rational product(rational r1, rational r2){ rational r; rational s1 = {r1.s, r1.n, r2.d}; rational s2 = {r2.s, r2.n, r1.d}; + /*Cancel as early as possible to avoid exceeding maximal value.*/ rational t1 = cancel(s1); rational t2 = cancel(s2); r.s = t1.s^t2.s; -- cgit v1.2.3