Skip to content

Commit

Permalink
Allow sqrt to downcast
Browse files Browse the repository at this point in the history
  • Loading branch information
yitzchak committed Sep 3, 2024
1 parent 02e6d6f commit abf2242
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions include/clasp/core/numbers.h
Original file line number Diff line number Diff line change
Expand Up @@ -706,11 +706,11 @@ inline Number_sp clasp_divide(Number_sp na, Number_sp nb) { return contagion_div

inline int clasp_number_compare(Number_sp x, Number_sp y) { return basic_compare(x, y); };

template<typename Float> inline Number_sp _sqrt(Float f) {
template<typename Float1, typename Float2 = Float1> inline Number_sp _sqrt(Float1 f) {
if (std::signbit(f))
return Complex_O::create(make_number(Float{0.0}), make_number(std::sqrt(-f)));
return Complex_O::create(make_number(Float2{0.0}), make_number(static_cast<Float2>(std::sqrt(-f))));

return make_number(std::sqrt(f));
return make_number(static_cast<Float2>(std::sqrt(f)));
}

inline Number_sp clasp_log1(Number_sp x) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/numbers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1768,7 +1768,7 @@ Number_sp Ratio_O::signum_() const {
return clasp_signum(this->_numerator);
}

Number_sp Ratio_O::sqrt_() const { return _sqrt(this->as_float_()); }
Number_sp Ratio_O::sqrt_() const { return _sqrt<double, float>(this->as_double_()); }

Number_sp Ratio_O::reciprocal_() const {
Integer_sp num = this->_numerator, denom = this->_denominator;
Expand Down Expand Up @@ -1894,7 +1894,7 @@ Number_sp LongFloat_O::sqrt_() const { return _sqrt(_Value); }

Number_sp Complex_O::sqrt_() const { return cl__expt(this->asSmartPtr(), _lisp->plusHalf()); }

Number_sp Bignum_O::sqrt_() const { return _sqrt(cast<float>()); }
Number_sp Bignum_O::sqrt_() const { return _sqrt<double, float>(cast<double>()); }

Number_sp Bignum_O::reciprocal_() const {
if (this->minusp_())
Expand Down

0 comments on commit abf2242

Please sign in to comment.