BashSpark
Loading...
Searching...
No Matches
bs::command_math Class Reference

Prints on stdout the result of a mathematical operation. More...

#include <command_math.h>

Inheritance diagram for bs::command_math:
Collaboration diagram for bs::command_math:

Public Member Functions

 command_math ()
 Constructs command.
 
shell_status run (const std::span< const std::string > &vArgs, shell_session &oSession) const override
 Prints on stdout the result of a mathematical operation.
 
virtual void msg_error_math (std::ostream &oStdErr, shell_status nStatus) const
 Displays the error message for math errors.
 
- Public Member Functions inherited from bs::command
 command (std::string sName)
 Construct a command with a given name.
 
virtual ~command ()=default
 Virtual destructor.
 
std::string get_name () const
 Get the command name (copy).
 
const std::string & get_name_ref () const noexcept
 Get the command name (reference).
 

Detailed Description

Prints on stdout the result of a mathematical operation.

Syntax: math mathematics expression

Supports:

  • Operators: + - * / ^ ** × ÷
  • Integer numbers within the range of bc2 64 bits
  • Parentheses: ( ) (and nested parentheses).
  • Negative powers partially: { x != 0 } ^ { y < 0 } = 0
  • Functions:
    • factorial(x)
    • abs(x): |x|
    • sign(x): x != 0: x / abs(x), x == 0: 0
    • sum(variable; start; end; step; expression) where expression is f(variable), ex: count = sum(x;1;1;100;1)
    • product(variable; start; end; step; expression) where expression is f(variable), ex: factorial = product(x;1;1;100;x)
    • Note that sum/product with same variable, the variable from the one inside will hide the variable of the one outside
    • Note that sum/product require variable names acceptable by the shell

Possible errors:

Member Function Documentation

◆ msg_error_math()

void bs::command_math::msg_error_math ( std::ostream &  oStdErr,
shell_status  nStatus 
) const
virtual

Displays the error message for math errors.

Can be overwritten with custom behaviour.

Parameters
oStdErrError output stream
nStatusStatus code

◆ run()

shell_status bs::command_math::run ( const std::span< const std::string > &  vArgs,
shell_session oSession 
) const
overridevirtual

Prints on stdout the result of a mathematical operation.

Parameters
vArgsArguments for the command.
oSessionThe shell session context.
Returns
Status of command execution.

Implements bs::command.


The documentation for this class was generated from the following files: