Fonts for Math in LaTeX

Similar to fonts for textual content, there are formatting styles or fonts that can be used on math expressions in LaTeX. For example, the set of real numbers R is typically written with what is called a blackboard bold font.

The formatting styles for math in LaTeX are:

mathnormal: This is the default for math expressions. The letters are italicized a bit. No need to apply it explicitly.
mathrm:     Non-italicized version of normal.
mathit:     Italicized like normal, but suits words better.
mathbf:     Bold. Use for vectors and so on.
mathsf:     Sans serif.
mathtt:     Fixed-width.
mathfrak:   Fraktur. Used for lie algebras.
mathcal:    Calligraphic.
mathbb:     Used for real sets.
mathscr:    Script.

A table of these fonts applied on symbols can be seen here.

Tried with: Ubuntu 14.04

Math errors in C++ and how to prevent or catch them

Math functions provided by the standard C++ library are defined in the cmath header file. Many of these functions require their input values to be in a specific domain. For example, the log function requires its input to be greater than zero. log(-3.14) or log(0) is not defined mathematically. The result of such a call is set to a special NaN, inf or -inf value. However, no exception is thrown and the call has actually failed silently. Note that this does not affect the correctness of future math function calls.

Preventing math error

There is no warning option provided by GCC that can be enabled to detect such errors in the code. Nor is there any compiler option to enable the standard C++ library to thrown an exception, say std::domain_error, when such an error occurs. Other libraries, like Boost, do throw such exceptions.

The only way to prevent these math errors is to check the bounds of the inputs passed to these functions. For example, you can write a wrapper function for each of such math functions that does the bounds checking and throws error or exception when the input is wrong. You can change your code to explicitly use only these math wrapper functions instead of those from cmath.

If the performance of this math code is important to you, then inline this function and enable the bounds checking only in Debug mode or when you want to check the correctness of some inputs. Disable the checking for Release mode.

Catching math error

When a math function is executed with wrong input, the errno variable from the cerrno header file is set to non-zero value. The list of error values can be seen here. Specifically, for math errors, it might be set to either ERANGE or EDOM. This refers to a range or domain error respectively. To get the error string explaining the error, use the std::strerror function.

For example:

// Checking for math error in C++
#include <cerrno>
#include <cmath>
#include <cstring>
#include <iostream>
std::cout << log(0) << std::endl;
if (errno)
std::cout << std::strerror(errno) << std::endl;
if (errno == EDOM)
std::cout << "Domain error\n";
if (errno == ERANGE)
std::cout << "Range error\n";

view raw
matherror.cpp
hosted with ❤ by GitHub

Reference: Section 40.3, The C++ Programming Language (4 Ed) by Stroustrup

Tried with: GCC 4.9.2 and Ubuntu 14.04

PowerShell: Math Functions

I keep a PowerShell console open at all times. I find it to be an excellent replacement for the Windows Calculator for quick calculations. Arithmetic operators are part of the PowerShell language, so they can be used directly at the prompt. For mathematical operations like square root, logarithm and those from trigonometry, the System.Math class from the .Net Framework can be used.

For example, to find the square root we can call the Sqrt() method from the System.Math class:

[System.Math]::Sqrt( 20 )

An easier way to do the same is:

[math]::sqrt( 20 )

This shorter case-insensitive version works because PowerShell is designed for an easy console experience. So, in PowerShell all the System.* classes can be invoked without the System prefix. Also, PowerShell tries to be as case-insensitive as possible, so most class and method names can be called ignoring the case.

Tried with: PowerShell 2
Reference: PowerShell in Action (2nd Edition) by Bruce Payette