fix square root of 0 bug

This commit is contained in:
Jeff Wang 2019-10-22 01:07:28 -04:00
parent 2816712d49
commit c3aa80289f
3 changed files with 11 additions and 2 deletions

View File

@ -139,13 +139,16 @@ void process_cmd(char cmd){
case '<':{ //use as +/- and sqrt case '<':{ //use as +/- and sqrt
if (IsShifted){ //take sqrt if (IsShifted){ //take sqrt
IsShifted = 0; IsShifted = 0;
if (!decn_is_nan(&stack(STACK_X))){ if (decn_is_zero(&stack(STACK_X))){
//sqrt(0) = 0
} else if (!decn_is_nan(&stack(STACK_X))){
copy_decn(&LastX, &stack(STACK_X)); //save LastX copy_decn(&LastX, &stack(STACK_X)); //save LastX
copy_decn(&AccDecn, &stack(STACK_X)); copy_decn(&AccDecn, &stack(STACK_X));
if (AccDecn.exponent < 0){ //negative if (AccDecn.exponent < 0){ //negative
set_dec80_NaN(&stack(STACK_X)); set_dec80_NaN(&stack(STACK_X));
break; break;
} }
//b = 0.5
set_dec80_zero(&BDecn); set_dec80_zero(&BDecn);
BDecn.lsu[0] = 5; BDecn.lsu[0] = 5;
pow_decn(); pow_decn();

View File

@ -363,7 +363,7 @@ void set_dec80_zero(dec80* dest){
} }
} }
static uint8_t decn_is_zero(const dec80* x){ uint8_t decn_is_zero(const dec80* x){
uint8_t i; uint8_t i;
for (i = 0; i < DEC80_NUM_LSU; i++){ for (i = 0; i < DEC80_NUM_LSU; i++){
if (x->lsu[i] != 0){ if (x->lsu[i] != 0){
@ -1047,6 +1047,11 @@ void exp_decn(void){
#define SAVED Tmp2Decn #define SAVED Tmp2Decn
#define NUM_TIMES Tmp3Decn #define NUM_TIMES Tmp3Decn
//check not error
if (decn_is_nan(&AccDecn)){
set_dec80_NaN(&AccDecn);
return;
}
//check if negative //check if negative
if (AccDecn.exponent < 0){ if (AccDecn.exponent < 0){
negate_decn(&AccDecn); negate_decn(&AccDecn);

View File

@ -64,6 +64,7 @@ void build_dec80(__xdata const char* signif_str, exp_t exponent);
void set_dec80_zero(dec80* dest); void set_dec80_zero(dec80* dest);
void set_dec80_NaN(dec80* dest); void set_dec80_NaN(dec80* dest);
uint8_t decn_is_zero(const dec80* x);
uint8_t decn_is_nan(const dec80* x); uint8_t decn_is_nan(const dec80* x);
void negate_decn(dec80* x); void negate_decn(dec80* x);