nourani / ScientificQuantities

A C++11 template class to deal with units inside the program
BSD 3-Clause "New" or "Revised" License
11 stars 2 forks source link

Dealing with sqrt and non-Natural bases #8

Open nourani opened 9 years ago

nourani commented 9 years ago

We can currently not deal with non-Natural bases. This can be an issue when using sqrt or other mathematical operations.

Example:

Area a = 25_m2;
Length l = sqrt(a);
=> error: conversion from ‘double’ to non-scalar type ‘SciQ::Length {aka SciQ::Quantity<1, 0, 0, 0, 0, 0, 0>}’ requested

Manual workaround

There is a manual workaround by keeping track of the units ourselves:

Area a = 25_m2;
Length l = sqrt(a)*meter;
=> length is 5 m

Downside is of course that we need to keep track of what happens to the units.

Potential solution

A potential solution seams to be to use the std::ratio to represent the base instead of just int. It would look like this:

template<class L, class M, class T, class EC, class TT, class AS, class LI>
class Quantity {
// Everything more or less as before
};

using Length = Quantity< ratio<1,1>, ratio<0,1>, ratio<0,1>, ratio<0,1>, ratio<0,1>, ratio<0,1>, ratio<0,1> >;

The main things that has to be changed will be the operator *,/,+,-,==,!=,>=,<=, << overloads to correctly manipulate these ratios.

I suggest branching this off

nourani commented 9 years ago

@crayzeewulf I have created a new version based on the above "issue". Can you verify that I haven't broken anything you are working on?