forked from Drexel-University-SPS/SIMFLEX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathFunctions.h
58 lines (43 loc) · 936 Bytes
/
MathFunctions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Mathematical functions
// Ed Callaghan
// November 2014
#ifndef MATHFUNCTIONS_H
#define MATHFUNCTIONS_H
#include <cmath>
#include <string> // is this even necessary?
#include <stdlib.h>
#include <time.h>
using namespace std ;
double randScale()
{
return rand()/RAND_MAX ;
}
int randInt(int lower, int upper)
{
return lower + rand() % upper ;
}
void to_string() {} ; // used to force compilation of sciForm()
// DOES NOT WORK
template <class T>
string sciForm(T input, int precision)
{
double digitCount ;
string inString = to_string(input) ;
string outString = inString[0] + '.' ;
int index = 1 ;
while (digitCount < precision)
{
char currentDigit = inString[index] ;
if (currentDigit != '.')
{
outString += currentDigit ;
if (currentDigit != '-')
digitCount++ ;
}
index++ ;
}
string ordOfMag = to_string(floor(log10(input))) ;
outString += 'E' + ordOfMag ;
return outString ;
}
#endif