Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
- fixed PrimitiveTokenType::Variable failing in to_rpn
- primitive_tokenizer no longer uses recursion
- remove PrimitiveTokenType::Boolean
- add the args for specifying the output base for numbers
- print variable map at an offset
  • Loading branch information
radj307 committed Jan 15, 2024
1 parent 5fa83de commit c703881
Show file tree
Hide file tree
Showing 9 changed files with 404 additions and 321 deletions.
2 changes: 1 addition & 1 deletion 307lib
149 changes: 64 additions & 85 deletions calc/FunctionMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

// 307lib
#include <var.hpp> //< for concepts
#include <print_table.hpp> //< for term::print_table

// stl
#include <cmath> //< for math functions
Expand All @@ -18,83 +19,92 @@ namespace calc {
class FunctionMap {
std::map<std::string, std::tuple<base_func*, std::string>> map{
/// Trigonometric Functions
std::make_pair("cos", std::make_tuple(new func(cosl), "Cosine")),
std::make_pair("sin", std::make_tuple(new func(sinl), "Sine")),
std::make_pair("tan", std::make_tuple(new func(tanl), "Tangent")),
std::make_pair("acos", std::make_tuple(new func(acosl), "Inverse Cosine")),
std::make_pair("asin", std::make_tuple(new func(asinl), "Inverse Sine")),
std::make_pair("atan", std::make_tuple(new func(atanl), "Inverse Tangent")),
std::make_pair("atan2", std::make_tuple(new func(atan2l), "Binary Tangent")),
std::make_pair("cos", std::make_tuple(new func(cosl), "Compute cosine")),
std::make_pair("sin", std::make_tuple(new func(sinl), "Compute sine")),
std::make_pair("tan", std::make_tuple(new func(tanl), "Compute tangent")),
std::make_pair("acos", std::make_tuple(new func(acosl), "Compute arc cosine")),
std::make_pair("asin", std::make_tuple(new func(asinl), "Compute arc sine")),
std::make_pair("atan", std::make_tuple(new func(atanl), "Compute arc tangent")),
std::make_pair("atan2", std::make_tuple(new func(atan2l), "Compute arc tangent with two parameters")),

/// Hyperbolic Functions
std::make_pair("cosh", std::make_tuple(new func(coshl), "Hyperbolic Cosine")),
std::make_pair("sinh", std::make_tuple(new func(sinhl), "Hyperbolic Sine")),
std::make_pair("tanh", std::make_tuple(new func(tanhl), "Hyperbolic Tangent")),
std::make_pair("acosh", std::make_tuple(new func(acoshl), "Inverse Hyperbolic Cosine")),
std::make_pair("asinh", std::make_tuple(new func(asinhl), "Inverse Hyperbolic Sine")),
std::make_pair("atanh", std::make_tuple(new func(atanhl), "Inverse Hyperbolic Tangent")),
std::make_pair("cosh", std::make_tuple(new func(coshl), "Compute hyperbolic cosine")),
std::make_pair("sinh", std::make_tuple(new func(sinhl), "Compute hyperbolic sine")),
std::make_pair("tanh", std::make_tuple(new func(tanhl), "Compute hyperbolic tangent")),
std::make_pair("acosh", std::make_tuple(new func(acoshl), "Compute area hyperbolic cosine")),
std::make_pair("asinh", std::make_tuple(new func(asinhl), "Compute area hyperbolic sine")),
std::make_pair("atanh", std::make_tuple(new func(atanhl), "Compute area hyperbolic tangent")),

/// Exponential and Logarithmic Functions
std::make_pair("exp", std::make_tuple(new func(expl), "")),
//std::make_pair("frexp", std::make_tuple(new func( frexpl ), "")), //< uses pointers, must be adapted
std::make_pair("ldexp", std::make_tuple(new func(ldexpl), "")),
std::make_pair("log", std::make_tuple(new func(logl), "")),
std::make_pair("log10", std::make_tuple(new func(log10l), "")),
//std::make_pair("modf", std::make_tuple(new func( modfl ), "")), //< uses pointers, must be adapted
std::make_pair("exp2", std::make_tuple(new func(exp2l), "")),
std::make_pair("expm1", std::make_tuple(new func(expm1l), "")),
std::make_pair("ilogb", std::make_tuple(new func(ilogbl), "")),
std::make_pair("log1p", std::make_tuple(new func(log1pl), "")),
std::make_pair("log2", std::make_tuple(new func(log2l), "")),
std::make_pair("logb", std::make_tuple(new func(logbl), "")),
std::make_pair("scalbn", std::make_tuple(new func(scalbnl), "")),
std::make_pair("scalbln", std::make_tuple(new func(scalblnl), "")),
std::make_pair("exp", std::make_tuple(new func(expl), "Compute exponential function")),
//std::make_pair("frexp", std::make_tuple(new func( frexpl ), "Get significand and exponent")), //< uses pointers, must be adapted
std::make_pair("ldexp", std::make_tuple(new func(ldexpl), "Generate value from significand and exponent")),
std::make_pair("log", std::make_tuple(new func(logl), "Compute natural logarithm")),
std::make_pair("log10", std::make_tuple(new func(log10l), "Compute common logarithm")),
//std::make_pair("modf", std::make_tuple(new func( modfl ), "Break into fractional and integral parts")), //< uses pointers, must be adapted
std::make_pair("exp2", std::make_tuple(new func(exp2l), "Compute binary exponential function")),
std::make_pair("expm1", std::make_tuple(new func(expm1l), "Compute exponential minus one")),
std::make_pair("ilogb", std::make_tuple(new func(ilogbl), "Integer binary logarithm")),
std::make_pair("log1p", std::make_tuple(new func(log1pl), "Compute logarithm plus one")),
std::make_pair("log2", std::make_tuple(new func(log2l), "Compute binary logarithm")),
std::make_pair("logb", std::make_tuple(new func(logbl), "Compute floating-point base logarithm")),
std::make_pair("scalbn", std::make_tuple(new func(scalbnl), "Scale significand using floating-point base exponent")),
std::make_pair("scalbln", std::make_tuple(new func(scalblnl), "Scale significand using floating-point base exponent (long)")),

/// Power Functions
std::make_pair("pow", std::make_tuple(new func(std::function{ [](Number const& base, Number const& exp) -> Number {
if (base.has_integral_value() && exp.has_integral_value()) {
return ipow(base.cast_to<int64_t>(), exp.cast_to<int64_t>());
}
else return powl(base.cast_to<long double>(), exp.cast_to<long double>());
} }), "Calculates the result of an exponent.")),
std::make_pair("sqrt", std::make_tuple(new func(sqrtl), "Calculates a square root.")),
std::make_pair("cbrt", std::make_tuple(new func(cbrtl), "Calculates a cubic root.")),
std::make_pair("hypot", std::make_tuple(new func(hypotl), "Calculate hypotenuse.")),
} }), "Raise to power")),
std::make_pair("sqrt", std::make_tuple(new func(sqrtl), "Compute square root")),
std::make_pair("cbrt", std::make_tuple(new func(cbrtl), "Compute cubic root")),
std::make_pair("hypot", std::make_tuple(new func(hypotl), "Compute hypotenuse")),

/// Error & Gamma Functions
std::make_pair("erf", std::make_tuple(new func(erfl), "")),
std::make_pair("erfc", std::make_tuple(new func(erfcl), "")),
std::make_pair("tgamma", std::make_tuple(new func(tgammal), "")),
std::make_pair("lgamma", std::make_tuple(new func(lgammal), "")),
std::make_pair("erf", std::make_tuple(new func(erfl), "Compute error function")),
std::make_pair("erfc", std::make_tuple(new func(erfcl), "Compute complementary error function")),
std::make_pair("tgamma", std::make_tuple(new func(tgammal), "Compute gamma function")),
std::make_pair("lgamma", std::make_tuple(new func(lgammal), "Compute log-gamma function")),

/// Rounding & Remainder Functions
std::make_pair("ceil", std::make_tuple(new func(ceill), "Raises a number to the nearest integral.")),
std::make_pair("floor", std::make_tuple(new func(floorl), "Lowers a number to the nearest integral.")),
std::make_pair("fmod", std::make_tuple(new func(fmodl), "")), //< implemented as '%' operator
std::make_pair("trunc", std::make_tuple(new func(truncl), "Truncate a floating-point number.")),
std::make_pair("round", std::make_tuple(new func(roundl), "Round a number to the nearest integral.")),
std::make_pair("remainder", std::make_tuple(new func(remainderl), "Get remainder of division operation.")),
std::make_pair("ceil", std::make_tuple(new func(ceill), "Round up value")),
std::make_pair("floor", std::make_tuple(new func(floorl), "Round down value")),
std::make_pair("fmod", std::make_tuple(new func(std::function{ [](Number const& numer, Number const& denom) -> Number { return numer % denom; } }), "Compute remainder of division")),
std::make_pair("trunc", std::make_tuple(new func(truncl), "Truncate value")),
std::make_pair("round", std::make_tuple(new func(roundl), "Round to nearest")),
std::make_pair("nearbyint", std::make_tuple(new func(nearbyintl), "Round to nearby integral value")),
std::make_pair("remainder", std::make_tuple(new func(remainderl), "Compute remainder (IEC 60559)")),

/// Floating-point Manipulation Functions
std::make_pair("copysign", std::make_tuple(new func(copysignl), "")),
std::make_pair("nextafter", std::make_tuple(new func(nextafterl), "")),
std::make_pair("nexttoward", std::make_tuple(new func(nexttowardl), "")),
std::make_pair("copysign", std::make_tuple(new func(copysignl), "Copy sign")),
std::make_pair("nextafter", std::make_tuple(new func(nextafterl), "Next representable value")),
std::make_pair("nexttoward", std::make_tuple(new func(nexttowardl), "Next representable value toward precise value")),

/// Minimum, Maximum, & Difference Functions
//std::make_pair("dim", std::make_tuple(new func( fdiml ), "")), //< weird
std::make_pair("dim", std::make_tuple(new func(fdiml), "Positive difference")),
std::make_pair("max", std::make_tuple(new func(std::function{ [](Number const& a, Number const& b) -> Number {
if (a.has_integral_value() && b.has_integral_value()) {
return imax(a.cast_to<int64_t>(), b.cast_to<int64_t>());
}
else return fmaxl(a.cast_to<long double>(), b.cast_to<long double>());
} }), "Get Larger Value")),
} }), "Get larger value")),
std::make_pair("min", std::make_tuple(new func(std::function{ [](Number const& a, Number const& b) -> Number {
if (a.has_integral_value() && b.has_integral_value()) {
return imin(a.cast_to<int64_t>(), b.cast_to<int64_t>());
}
else return fminl(a.cast_to<long double>(), b.cast_to<long double>());
} }), "Get Smaller Value")),
} }), "Get smaller value")),

/// Other
std::make_pair("abs", std::make_tuple(new func(std::function{ [](Number const& n) -> Number {
if (n.has_integral_value()) {
return llabs(n.cast_to<long long>());
}
else return fabsl(n.cast_to<long double>());
} }), "Get Absolute Value")),
std::make_pair("fma", std::make_tuple(new func(fmal), "x * y + z.")),
std::make_pair("fma", std::make_tuple(new func(fmal), "Multiply-add")),

};

Expand Down Expand Up @@ -141,44 +151,13 @@ namespace calc {
return get(functionName)->getParamsCount();
}

friend std::ostream& operator<<(std::ostream& os, const FunctionMap& fnMap)
friend std::ostream& operator<<(std::ostream& os, const FunctionMap& m)
{
// get the longest name length
auto maxNameLen{ 0 };
auto maxDescLen{ 0 };
for (const auto& [name, val] : fnMap.map) {
if (const auto nameSize{ name.size() }; nameSize > maxNameLen)
maxNameLen = nameSize;
if (const auto descSize{ std::get<1>(val).size() }; descSize > maxDescLen)
maxDescLen = descSize;
}
// increment both max lengths to get column widths - 1
if (++maxNameLen < 9)
maxNameLen = 9;
if (++maxDescLen < 11)
maxDescLen = 11;

os // line 0:
<< "| Function " << indent(maxNameLen, 9)
<< "| Params "
<< "| Description" << indent(maxDescLen, 11)
<< '|' << '\n'
// line 1:
<< '|' << indent(maxNameLen + 1, 0, '-')
<< '|' << indent(8, 0, '-')
<< '|' << indent(maxDescLen + 1, 0, '-')
<< '|' << '\n';

for (const auto& [name, val] : fnMap.map) {
const auto& [func, desc] { val };
const auto paramsCountString{ str::stringify(func->getParamsCount()) };
os // line n:
<< "| " << name << indent(maxNameLen, name.size())
<< "| " << paramsCountString << indent(7, paramsCountString.size())
<< "| " << desc << indent(maxDescLen, desc.size())
<< '|' << '\n';
}
return os;
return os << term::print_table(m.map.begin(), m.map.end(), {
{ "Function", [](auto&& pr) { return pr.first; } },
{ "Param#", [](auto&& pr) { return std::to_string(std::get<0>(pr.second)->getParamsCount()); }, term::HorizontalAlignment::Center },
{ "Description", [](auto&& pr) { return std::get<1>(pr.second); } },
});
}
};
}
11 changes: 9 additions & 2 deletions calc/VarMap.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#pragma once
// libcalc
#include <Number.hpp>

// 307lib
#include <print_table.hpp>

// STL
#include <map>
#include <string>
#include <concepts>
Expand Down Expand Up @@ -40,8 +45,10 @@ namespace calc {

friend std::ostream& operator<<(std::ostream& os, VarMap const& vm)
{
// TODO
return os;
return os << term::print_table(vm.map.begin(), vm.map.end(), {
{ "Name", [](auto&& pr) { return pr.first; } },
{ "Value", [](auto&& pr) { return str::stringify(pr.second); } },
});
}
};
}
Loading

0 comments on commit c703881

Please sign in to comment.