diff options
| author | Julian Weigt <juw@posteo.de> | 2026-02-09 18:51:46 +0100 |
|---|---|---|
| committer | Julian Weigt <juw@posteo.de> | 2026-02-09 19:13:37 +0100 |
| commit | 15cb5ccbbee7abb8ce253632bc2c4febdfaedca5 (patch) | |
| tree | 6c335dac9be90fed6e57e6b0e471f928872fb094 /ratio.c | |
| parent | 3397d3040a63d3afd9392fa465c8b6e1380dce71 (diff) | |
Add more comments to code.
Diffstat (limited to 'ratio.c')
| -rw-r--r-- | ratio.c | 2 |
1 files changed, 2 insertions, 0 deletions
@@ -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; |
