Hash :
7f2f72e4
Author :
Thomas de Grivel
Date :
2024-03-04T14:19:12
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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
/* c3
* Copyright 2022-2024 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_ratio tmp;
s_integer i;
s_integer j;
assert(a);
assert(b);
assert(dest);
assert(integer_is_positive(&a->denominator));
assert(integer_is_positive(&b->denominator));
if (! integer_init(&tmp.numerator))
return NULL;
if (! integer_init(&tmp.denominator)) {
integer_clean(&tmp.numerator);
return NULL;
}
if (! integer_mul(&a->numerator, &b->denominator, &i)) {
ratio_clean(&tmp);
return NULL;
}
if (! integer_mul(&b->numerator, &a->denominator, &j)) {
integer_clean(&i);
ratio_clean(&tmp);
return NULL;
}
if (! integer_add(&i, &j, &tmp.numerator)) {
integer_clean(&i);
integer_clean(&j);
ratio_clean(&tmp);
return NULL;
}
if (! integer_mul(&a->denominator, &b->denominator,
&tmp.denominator)) {
integer_clean(&i);
integer_clean(&j);
ratio_clean(&tmp);
return NULL;
}
integer_clean(&i);
integer_clean(&j);
if (! ratio_simplify(&tmp, dest)) {
ratio_clean(&tmp);
return NULL;
}
ratio_clean(&tmp);
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)
{
s_ratio tmp = {0};
s_ratio tmp2 = {0};
assert(a);
assert(b);
assert(dest);
assert(! integer_is_zero(&b->numerator));
assert(integer_is_positive(&a->denominator));
assert(integer_is_positive(&b->denominator));
if (! integer_mul(&a->numerator, &b->denominator, &tmp.numerator))
return NULL;
if (! integer_mul(&a->denominator, &b->numerator, &tmp.denominator)) {
integer_clean(&tmp.numerator);
return NULL;
}
if (integer_is_negative(&tmp.denominator)) {
tmp2 = tmp;
if (! integer_neg(&tmp2.numerator, &tmp.numerator)) {
ratio_clean(&tmp2);
return NULL;
}
if (! integer_neg(&tmp2.denominator, &tmp.denominator)) {
integer_clean(&tmp.numerator);
ratio_clean(&tmp2);
return NULL;
}
ratio_clean(&tmp2);
}
if (! ratio_simplify(&tmp, dest)) {
ratio_clean(&tmp);
return NULL;
}
ratio_clean(&tmp);
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 *q, const char *p)
{
s_buf buf;
uw len;
sw r;
s_ratio tmp;
assert(q);
assert(p);
if (! p)
return NULL;
len = strlen(p);
buf_init(&buf, false, len, (char *) p); // buf is read-only
buf.wpos = len;
r = buf_parse_ratio(&buf, &tmp);
if (r < 0 || (uw) r != len) {
err_write_1("ratio_init_1: invalid ratio: \"");
err_write_1(p);
err_write_1("\", ");
err_inspect_uw(&len);
err_write_1(" != ");
err_inspect_sw(&r);
assert(! "ratio_init_1: invalid ratio");
return NULL;
}
*q = tmp;
return q;
}
s_ratio * ratio_init_copy (s_ratio *dest, const s_ratio *src)
{
s_ratio tmp = {0};
assert(src);
assert(dest);
if (! integer_init_copy(&tmp.numerator, &src->numerator))
return NULL;
if (! integer_init_copy(&tmp.denominator, &src->denominator)) {
integer_clean(&tmp.numerator);
return NULL;
}
*dest = tmp;
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)
{
s_ratio tmp = {0};
assert(a);
assert(b);
assert(dest);
assert(integer_is_positive(&a->denominator));
assert(integer_is_positive(&b->denominator));
if (! integer_mul(&a->numerator, &b->numerator, &tmp.numerator))
return NULL;
if (! integer_mul(&a->denominator, &b->denominator,
&tmp.denominator)) {
integer_clean(&tmp.numerator);
return NULL;
}
if (! ratio_simplify(&tmp, dest)) {
ratio_clean(&tmp);
return NULL;
}
ratio_clean(&tmp);
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_ratio * ratio_simplify (s_ratio *r, s_ratio *dest)
{
s_ratio tmp;
s_integer gcd;
assert(r);
assert(dest);
assert(integer_is_positive(&r->denominator));
if (! integer_gcd(&r->numerator, &r->denominator, &gcd))
return NULL;
if (! integer_div(&r->numerator, &gcd, &tmp.numerator)) {
integer_clean(&gcd);
return NULL;
}
if (! integer_div(&r->denominator, &gcd, &tmp.denominator)) {
integer_clean(&tmp.numerator);
integer_clean(&gcd);
return NULL;
}
integer_clean(&gcd);
*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)
{
s_ratio tmp;
s_integer i;
s_integer j;
assert(a);
assert(b);
assert(dest);
assert(integer_is_positive(&a->denominator));
assert(integer_is_positive(&b->denominator));
if (! integer_init(&tmp.numerator))
return NULL;
if (! integer_init(&tmp.denominator)) {
integer_clean(&tmp.numerator);
return NULL;
}
if (! integer_mul(&a->numerator, &b->denominator, &i)) {
ratio_clean(&tmp);
return NULL;
}
if (! integer_mul(&b->numerator, &a->denominator, &j)) {
integer_clean(&i);
ratio_clean(&tmp);
return NULL;
}
if (! integer_sub(&i, &j, &tmp.numerator)) {
integer_clean(&i);
integer_clean(&j);
ratio_clean(&tmp);
return NULL;
}
integer_clean(&i);
integer_clean(&j);
if (! integer_mul(&a->denominator, &b->denominator,
&tmp.denominator)) {
ratio_clean(&tmp);
return NULL;
}
if (! ratio_simplify(&tmp, dest)) {
ratio_clean(&tmp);
return NULL;
}
ratio_clean(&tmp);
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;
}