From 75b2534589ae75df3f63b3652283a9405be18314 Mon Sep 17 00:00:00 2001 From: David McMackins II Date: Fri, 2 Sep 2016 07:12:03 -0500 Subject: Add degrees operator --- doc/hotkeys.md | 2 ++ src/keys.c | 3 +++ src/tibchar.c | 2 +- src/tibeval.c | 13 +++++++------ src/tibtype.c | 29 +++++++++++++++++++++++++++++ src/tibtype.h | 3 +++ 6 files changed, 45 insertions(+), 7 deletions(-) diff --git a/doc/hotkeys.md b/doc/hotkeys.md index 8fb71a2..e4c6d63 100644 --- a/doc/hotkeys.md +++ b/doc/hotkeys.md @@ -26,9 +26,11 @@ Hotkey List ----------- - `c`: `cos(` +- `C-d`: `°` - `e`: `e` (constant) - `C-e`: `E (*10^)` - `i`: `i` (constant) +- `C-p`: `π` (constant) - `s`: `sin(` - `t`: `tan(` - `$`: `→` (stores value to left into variable to right) diff --git a/src/keys.c b/src/keys.c index 2d00e6d..b87a295 100644 --- a/src/keys.c +++ b/src/keys.c @@ -43,6 +43,9 @@ normalize_keycode (SDL_Keycode code, SDL_Keymod mod) case SDLK_a: return TIB_CHAR_ANS; + case SDLK_d: + return TIB_CHAR_DEGREE; + case SDLK_e: return TIB_CHAR_EPOW10; diff --git a/src/tibchar.c b/src/tibchar.c index ec9b3ee..876cc77 100644 --- a/src/tibchar.c +++ b/src/tibchar.c @@ -52,7 +52,7 @@ tib_special_char_text (int c) return "cos("; case TIB_CHAR_DEGREE: - return "*(180/pi)"; + return "*(pi/180)"; case TIB_CHAR_DELVAR: return "DelVar"; diff --git a/src/tibeval.c b/src/tibeval.c index 59deb3f..43c93f0 100644 --- a/src/tibeval.c +++ b/src/tibeval.c @@ -47,12 +47,13 @@ struct math_operator static const struct math_operator OPERATORS[] = { - { { .t = tib_factorial }, T, '!', 0 }, - { { .tt = tib_pow }, TT, '^', 1 }, - { { .tt = tib_mul }, TT, '*', 2 }, - { { .tt = tib_div }, TT, '/', 2 }, - { { .tt = tib_add }, TT, '+', 3 }, - { { .tt = tib_sub }, TT, '-', 3 } + { { .t = tib_factorial }, T, '!', 0 }, + { { .t = tib_toradians }, T, TIB_CHAR_DEGREE, 0 }, + { { .tt = tib_pow }, TT, '^', 1 }, + { { .tt = tib_mul }, TT, '*', 2 }, + { { .tt = tib_div }, TT, '/', 2 }, + { { .tt = tib_add }, TT, '+', 3 }, + { { .tt = tib_sub }, TT, '-', 3 } }; #define NUM_MATH_OPERATORS (sizeof OPERATORS / sizeof (struct math_operator)) diff --git a/src/tibtype.c b/src/tibtype.c index 3c04396..45bbce0 100644 --- a/src/tibtype.c +++ b/src/tibtype.c @@ -23,8 +23,10 @@ #include #include +#include "tibchar.h" #include "tiberr.h" #include "tibtype.h" +#include "tibvar.h" #include "util.h" TIB * @@ -1097,3 +1099,30 @@ tib_factorial (const TIB *t) return tib_new_complex (gsl_sf_gamma (GSL_REAL (t->value.number) + 1), 0); } + +TIB * +tib_toradians (const TIB *t) +{ + TIB *pi = tib_var_get (TIB_CHAR_PI); + if (!pi) + return NULL; + + TIB *factor = tib_new_complex (180, 0); + if (!factor) + { + tib_decref (pi); + return NULL; + } + + TIB *temp = tib_div (pi, factor); + tib_decref (pi); + tib_decref (factor); + if (!temp) + return NULL; + + factor = temp; + temp = tib_mul (t, factor); + tib_decref (factor); + + return temp; +} diff --git a/src/tibtype.h b/src/tibtype.h index 7a88afd..68549b7 100644 --- a/src/tibtype.h +++ b/src/tibtype.h @@ -116,4 +116,7 @@ tib_factorial (const TIB *t); TIB * tib_log (const TIB *t); +TIB * +tib_toradians (const TIB *t); + #endif -- cgit v1.2.3