Post Scarcity
A prototype for a post scarcity programming environment
Loading...
Searching...
No Matches
ratio.c File Reference
#include <math.h>
#include <stdio.h>
#include "arith/integer.h"
#include "arith/peano.h"
#include "arith/ratio.h"
#include "arith/real.h"
#include "debug.h"
#include "io/print.h"
#include "memory/conspage.h"
#include "memory/consspaceobject.h"
#include "memory/stack.h"
#include "ops/equal.h"
#include "ops/lispops.h"
Include dependency graph for ratio.c:

Go to the source code of this file.

Macros

#define _GNU_SOURCE
 

Functions

struct cons_pointer add_integer_ratio (struct cons_pointer intarg, struct cons_pointer ratarg)
 return a cons_pointer indicating a number which is the sum of the intger indicated by intarg and the ratio indicated by ratarg.
 
struct cons_pointer add_ratio_ratio (struct cons_pointer arg1, struct cons_pointer arg2)
 return a cons_pointer indicating a number which is the sum of the ratios indicated by arg1 and arg2.
 
long double c_ratio_to_ld (struct cons_pointer rat)
 convert a ratio to an equivalent long double.
 
struct cons_pointer divide_ratio_ratio (struct cons_pointer arg1, struct cons_pointer arg2)
 return a cons_pointer to a ratio which represents the value of the ratio indicated by arg1 divided by the ratio indicated by arg2.
 
bool equal_ratio_ratio (struct cons_pointer a, struct cons_pointer b)
 True if a and be are identical rationals, else false.
 
int64_t greatest_common_divisor (int64_t m, int64_t n)
 return, as an int64_t, the greatest common divisor of m and n,
 
int64_t least_common_multiple (int64_t m, int64_t n)
 return, as an int64_t, the least common multiple of m and n,
 
struct cons_pointer make_ratio (struct cons_pointer dividend, struct cons_pointer divisor, bool simplify)
 Construct a ratio frame from this dividend and divisor, expected to be integers, in the context of the stack_frame indicated by this frame_pointer.
 
struct cons_pointer multiply_integer_ratio (struct cons_pointer intarg, struct cons_pointer ratarg)
 return a cons_pointer indicating a number which is the product of the intger indicated by intarg and the ratio indicated by ratarg.
 
struct cons_pointer multiply_ratio_ratio (struct cons_pointer arg1, struct cons_pointer arg2)
 return a cons_pointer indicating a number which is the product of the ratios indicated by arg1 and arg2.
 
struct cons_pointer simplify_ratio (struct cons_pointer pointer)
 ratio.h
 
struct cons_pointer subtract_ratio_ratio (struct cons_pointer arg1, struct cons_pointer arg2)
 return a cons_pointer indicating a number which is the difference of the ratios indicated by arg1 and arg2.
 

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 10 of file ratio.c.

Function Documentation

◆ add_integer_ratio()

struct cons_pointer add_integer_ratio ( struct cons_pointer  intarg,
struct cons_pointer  ratarg 
)

return a cons_pointer indicating a number which is the sum of the intger indicated by intarg and the ratio indicated by ratarg.

Exceptions
ifeither `intarg` or `ratarg` is not of the expected type.

Definition at line 137 of file ratio.c.

References acquire_integer(), add_ratio_ratio(), c_string_to_lisp_string(), DEBUG_ARITH, debug_print(), debug_print_object(), dec_ref(), integerp, make_cons(), make_ratio(), NIL, ratiop, release_integer(), and throw_exception().

Referenced by add_2().

◆ add_ratio_ratio()

struct cons_pointer add_ratio_ratio ( struct cons_pointer  arg1,
struct cons_pointer  arg2 
)

return a cons_pointer indicating a number which is the sum of the ratios indicated by arg1 and arg2.

Exceptions
willreturn an exception if either `arg1` or `arg2` is not a rational number.

Definition at line 93 of file ratio.c.

References add_integers(), c_string_to_lisp_string(), DEBUG_ARITH, debug_print(), debug_print_object(), make_cons(), make_ratio(), multiply_integers(), NIL, cons_space_object::payload, pointer2cell, ratiop, and throw_exception().

Referenced by add_2(), add_integer_ratio(), and subtract_ratio_ratio().

◆ c_ratio_to_ld()

long double c_ratio_to_ld ( struct cons_pointer  rat)

convert a ratio to an equivalent long double.

Parameters
rata pointer to a ratio.
Returns
long double

Definition at line 379 of file ratio.c.

References DEBUG_ARITH, debug_print(), debug_print_object(), debug_printf(), nilp, cons_space_object::payload, pointer2cell, and ratiop.

Referenced by equal_real_number(), and lisp_ratio_to_real().

◆ divide_ratio_ratio()

struct cons_pointer divide_ratio_ratio ( struct cons_pointer  arg1,
struct cons_pointer  arg2 
)

return a cons_pointer to a ratio which represents the value of the ratio indicated by arg1 divided by the ratio indicated by arg2.

Exceptions
willreturn an exception if either `arg1` or `arg2` is not a rational number.

Definition at line 176 of file ratio.c.

References DEBUG_ARITH, debug_print(), debug_print_object(), dec_ref(), make_ratio(), multiply_ratio_ratio(), and pointer2cell.

Referenced by lisp_divide().

◆ equal_ratio_ratio()

bool equal_ratio_ratio ( struct cons_pointer  a,
struct cons_pointer  b 
)

True if a and be are identical rationals, else false.

TODO: we need ways of checking whether rationals are equal to floats and to integers.

Definition at line 357 of file ratio.c.

References equal_integer_integer(), cons_space_object::payload, pointer2cell, and ratiop.

Referenced by equal(), and equal_number_number().

◆ greatest_common_divisor()

int64_t greatest_common_divisor ( int64_t  m,
int64_t  n 
)

return, as an int64_t, the greatest common divisor of m and n,

Definition at line 30 of file ratio.c.

Referenced by least_common_multiple(), and simplify_ratio().

◆ least_common_multiple()

int64_t least_common_multiple ( int64_t  m,
int64_t  n 
)

return, as an int64_t, the least common multiple of m and n,

Definition at line 44 of file ratio.c.

References greatest_common_divisor().

◆ make_ratio()

struct cons_pointer make_ratio ( struct cons_pointer  dividend,
struct cons_pointer  divisor,
bool  simplify 
)

Construct a ratio frame from this dividend and divisor, expected to be integers, in the context of the stack_frame indicated by this frame_pointer.

Exceptions
ifeither `dividend` or `divisor` is not an integer.

Definition at line 312 of file ratio.c.

References allocate_cell(), c_string_to_lisp_string(), DEBUG_ALLOC, debug_print(), debug_print_object(), debug_printf(), debug_println(), dec_ref(), eq(), inc_ref(), integerp, NIL, cons_space_object::payload, pointer2cell, RATIOTV, simplify_ratio(), and throw_exception().

Referenced by absolute(), add_integer_ratio(), add_ratio_ratio(), divide_ratio_ratio(), lisp_divide(), multiply_integer_ratio(), multiply_ratio_ratio(), negative(), read_number(), simplify_ratio(), and subtract_2().

◆ multiply_integer_ratio()

struct cons_pointer multiply_integer_ratio ( struct cons_pointer  intarg,
struct cons_pointer  ratarg 
)

return a cons_pointer indicating a number which is the product of the intger indicated by intarg and the ratio indicated by ratarg.

Exceptions
ifeither `intarg` or `ratarg` is not of the expected type.

Definition at line 254 of file ratio.c.

References acquire_integer(), c_string_to_lisp_string(), DEBUG_ARITH, debug_print(), debug_print_object(), integerp, make_ratio(), multiply_ratio_ratio(), NIL, ratiop, release_integer(), and throw_exception().

Referenced by multiply_2().

◆ multiply_ratio_ratio()

struct cons_pointer multiply_ratio_ratio ( struct cons_pointer  arg1,
struct cons_pointer  arg2 
)

return a cons_pointer indicating a number which is the product of the ratios indicated by arg1 and arg2.

Exceptions
willreturn an exception if either `arg1` or `arg2` is not a rational number.

Definition at line 203 of file ratio.c.

References acquire_integer(), c_string_to_lisp_string(), DEBUG_ARITH, debug_print(), debug_print_object(), make_ratio(), NIL, cons_space_object::payload, pointer2cell, ratiop, release_integer(), and throw_exception().

Referenced by divide_ratio_ratio(), multiply_2(), and multiply_integer_ratio().

◆ simplify_ratio()

struct cons_pointer simplify_ratio ( struct cons_pointer  arg)

ratio.h

functions for rational number cells.

(c) 2017 Simon Brooke simon.nosp@m.@jou.nosp@m.rneym.nosp@m.an.c.nosp@m.c Licensed under GPL version 2.0, or, at your option, any later version.

Definition at line 48 of file ratio.c.

References acquire_integer(), DEBUG_ARITH, debug_printf(), greatest_common_divisor(), make_ratio(), NIL, cons_space_object::payload, pointer2cell, and ratiop.

Referenced by make_ratio().

◆ subtract_ratio_ratio()

struct cons_pointer subtract_ratio_ratio ( struct cons_pointer  arg1,
struct cons_pointer  arg2 
)

return a cons_pointer indicating a number which is the difference of the ratios indicated by arg1 and arg2.

Exceptions
willreturn an exception if either `arg1` or `arg2` is not a rational number.

Definition at line 290 of file ratio.c.

References add_ratio_ratio(), DEBUG_ARITH, debug_print(), debug_print_object(), dec_ref(), and negative().

Referenced by subtract_2().