Hash :
1d7cec70
Author :
Thomas de Grivel
Date :
2024-03-01T12:01:11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
/* c3
* Copyright 2022,2023 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include "assert.h"
#include <string.h>
#include <stdlib.h>
#include "buf.h"
#include "buf_parse.h"
#include "buf_inspect.h"
#include "compare.h"
#include "integer.h"
#include "ratio.h"
s_ratio * ratio_add (const s_ratio *a, const s_ratio *b,
s_ratio *dest)
{
s_integer i;
s_integer j;
assert(a);
assert(b);
assert(dest);
assert(integer_is_positive(&a->denominator));
assert(integer_is_positive(&b->denominator));
integer_mul(&a->numerator, &b->denominator, &i);
integer_mul(&b->numerator, &a->denominator, &j);
integer_add(&i, &j, &dest->numerator);
integer_mul(&a->denominator, &b->denominator,
&dest->denominator);
integer_clean(&i);
integer_clean(&j);
return dest;
}
void ratio_clean (s_ratio *r)
{
assert(r);
integer_clean(&r->numerator);
integer_clean(&r->denominator);
}
s_ratio * ratio_div (const s_ratio *a, const s_ratio *b,
s_ratio *dest)
{
assert(a);
assert(b);
assert(dest);
assert(! integer_is_zero(&b->numerator));
assert(integer_is_positive(&a->denominator));
assert(integer_is_positive(&b->denominator));
integer_mul(&a->numerator, &b->denominator, &dest->numerator);
integer_mul(&a->denominator, &b->numerator, &dest->denominator);
return dest;
}
s_ratio * ratio_init (s_ratio *dest)
{
s_ratio tmp = {0};
assert(dest);
if (! integer_init(&tmp.numerator))
return NULL;
if (! integer_init(&tmp.denominator)) {
integer_clean(&tmp.numerator);
return NULL;
}
*dest = tmp;
return dest;
}
s_ratio * ratio_init_1 (s_ratio *r, const char *p)
{
s_integer numerator;
s_integer denominator;
assert(r);
assert(p);
// FIXME
integer_init_1(&numerator, p);
integer_init_u64(&denominator, 1);
ratio_init_integer(r, &numerator, &denominator);
return r;
}
s_ratio * ratio_init_copy (s_ratio *dest, const s_ratio *src)
{
assert(src);
assert(dest);
if (integer_init_copy(&dest->numerator, &src->numerator) == NULL)
return NULL;
if (integer_init_copy(&dest->denominator, &src->denominator) == NULL)
return NULL;
return dest;
}
s_ratio * ratio_init_integer (s_ratio *r, s_integer *numerator, s_integer *denominator)
{
s_ratio tmp = {0};
assert(numerator);
assert(denominator);
assert(r);
if (! integer_is_positive(denominator)) {
err_puts("ratio_init_integer: invalid denominator");
assert(! "ratio_init_integer: invalid denominator");
return NULL;
}
if (! integer_init_copy(&tmp.numerator, numerator))
return NULL;
if (! integer_init_copy(&tmp.denominator, denominator)) {
integer_clean(&tmp.numerator);
return NULL;
}
*r = tmp;
return r;
}
s_ratio * ratio_init_zero (s_ratio *r)
{
s_ratio tmp = {0};
assert(r);
if (! integer_init_zero(&tmp.numerator))
return NULL;
if (! integer_init_u8(&tmp.denominator, 1)) {
integer_clean(&tmp.numerator);
return NULL;
}
*r = tmp;
return r;
}
s_str * ratio_inspect (const s_ratio *src, s_str *dest)
{
s_buf buf;
sw r;
sw size;
assert(src);
assert(dest);
assert(integer_is_positive(&src->denominator));
size = buf_inspect_ratio_size(src);
if (size <= 0)
return NULL;
if (! buf_init_alloc(&buf, size))
return NULL;
r = buf_inspect_ratio(&buf, src);
if (r != size) {
err_write_1("ratio_inspect: ");
err_inspect_sw(&r);
err_write_1(" != ");
err_inspect_sw(&size);
err_write_1("\n");
assert(! "ratio_inspect: invalid ratio");
return NULL;
}
assert(buf.wpos == (uw) size);
return buf_to_str(&buf, dest);
}
bool ratio_is_negative (const s_ratio *r)
{
assert(r);
assert(integer_is_positive(&r->denominator));
return integer_is_negative(&r->numerator);
}
bool ratio_is_zero (const s_ratio *r)
{
assert(r);
assert(integer_is_positive(&r->denominator));
return integer_is_zero(&r->numerator);
}
s_ratio * ratio_mul (const s_ratio *a, const s_ratio *b,
s_ratio *dest)
{
assert(a);
assert(b);
assert(dest);
assert(integer_is_positive(&a->denominator));
assert(integer_is_positive(&b->denominator));
integer_mul(&a->numerator, &b->numerator, &dest->numerator);
integer_mul(&a->denominator, &b->denominator,
&dest->denominator);
return dest;
}
s_ratio * ratio_neg (const s_ratio *r, s_ratio *dest)
{
s_ratio tmp = {0};
assert(r);
assert(dest);
assert(integer_is_positive(&r->denominator));
if (! integer_neg(&r->numerator, &tmp.numerator))
return NULL;
if (! integer_init_copy(&tmp.denominator, &r->denominator)) {
integer_clean(&tmp.numerator);
return NULL;
}
*dest = tmp;
return dest;
}
s_tag * ratio_sqrt (const s_ratio *r, s_tag *dest)
{
s_ratio tmp = {0};
assert(r);
assert(dest);
assert(integer_is_positive(&r->denominator));
if (ratio_is_negative(r)) {
// FIXME
err_puts("ratio_sqrt: not implemented");
assert(! "ratio_sqrt: not implemented");
return NULL;
}
if (! integer_sqrt_positive(&r->numerator, &tmp.numerator))
return NULL;
if (! integer_sqrt_positive(&r->denominator, &tmp.denominator)) {
integer_clean(&tmp.numerator);
return NULL;
}
dest->type = TAG_RATIO;
dest->data.ratio = tmp;
return dest;
}
s_ratio * ratio_sub (const s_ratio *a, const s_ratio *b,
s_ratio *dest)
{
assert(a);
assert(b);
assert(dest);
assert(!integer_is_zero(&a->denominator));
assert(!integer_is_zero(&b->denominator));
s_integer temp1, temp2;
integer_mul(&a->numerator, &b->denominator, &temp1);
integer_mul(&b->numerator, &a->denominator, &temp2);
integer_sub(&temp1, &temp2, &dest->numerator);
integer_mul(&a->denominator, &b->denominator,
&dest->denominator);
return dest;
}
f32 ratio_to_f32(const s_ratio *r)
{
assert(r);
assert(integer_is_positive(&r->denominator));
f32 numerator = integer_to_f32(&r->numerator);
f32 denominator = integer_to_f32(&r->denominator);
return numerator / denominator;
}
f64 ratio_to_f64(const s_ratio *r)
{
assert(r);
assert(integer_is_positive(&r->denominator));
f64 numerator = integer_to_f64(&r->numerator);
f64 denominator = integer_to_f64(&r->denominator);
return numerator / denominator;
}
s8 ratio_to_s8 (const s_ratio *r)
{
assert(r);
assert(integer_is_positive(&r->denominator));
s8 numerator = integer_to_s8(&r->numerator);
s8 denominator = integer_to_s8(&r->denominator);
return numerator / denominator;
}
s16 ratio_to_s16 (const s_ratio *r)
{
assert(r);
assert(integer_is_positive(&r->denominator));
s16 numerator = integer_to_s16(&r->numerator);
s16 denominator = integer_to_s16(&r->denominator);
return numerator / denominator;
}
s32 ratio_to_s32 (const s_ratio *r)
{
assert(r);
assert(integer_is_positive(&r->denominator));
s32 numerator = integer_to_s32(&r->numerator);
s32 denominator = integer_to_s32(&r->denominator);
return numerator / denominator;
}
s64 ratio_to_s64 (const s_ratio *r)
{
assert(r);
assert(integer_is_positive(&r->denominator));
s64 numerator = integer_to_s64(&r->numerator);
s64 denominator = integer_to_s64(&r->denominator);
return numerator / denominator;
}
sw ratio_to_sw (const s_ratio *r)
{
assert(r);
assert(integer_is_positive(&r->denominator));
sw numerator = integer_to_sw(&r->numerator);
sw denominator = integer_to_sw(&r->denominator);
return numerator / denominator;
}
u8 ratio_to_u8 (const s_ratio *r)
{
assert(r);
assert(integer_is_positive(&r->denominator));
u8 numerator = integer_to_u8(&r->numerator);
u8 denominator = integer_to_u8(&r->denominator);
return numerator / denominator;
}
u16 ratio_to_u16 (const s_ratio *r)
{
assert(r);
assert(integer_is_positive(&r->denominator));
u16 numerator = integer_to_u16(&r->numerator);
u16 denominator = integer_to_u16(&r->denominator);
return numerator / denominator;
}
u32 ratio_to_u32 (const s_ratio *r)
{
assert(r);
assert(integer_is_positive(&r->denominator));
u32 numerator = integer_to_u32(&r->numerator);
u32 denominator = integer_to_u32(&r->denominator);
return numerator / denominator;
}
u64 ratio_to_u64 (const s_ratio *r)
{
assert(r);
assert(integer_is_positive(&r->denominator));
u64 numerator = integer_to_u64(&r->numerator);
u64 denominator = integer_to_u64(&r->denominator);
return numerator / denominator;
}
uw ratio_to_uw (const s_ratio *r)
{
assert(r);
assert(integer_is_positive(&r->denominator));
uw numerator = integer_to_uw(&r->numerator);
uw denominator = integer_to_uw(&r->denominator);
return numerator / denominator;
}