Last active
December 26, 2019 05:20
-
-
Save Rubix982/efd485dbe61328b7cd2ab4b8dd1ceaa8 to your computer and use it in GitHub Desktop.
Implementation of Algorithm to calculate log of any base to any precision, using the technique described here: https://math.stackexchange.com/questions/820094/what-is-the-best-way-to-calculate-log-without-a-calculator#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <math.h> | |
#include <string> | |
using namespace std; | |
int main(void) { | |
uint_fast64_t b, p; | |
double start = 2; | |
// first input 'b' is the base, second input 'p' is the precision | |
cin >> b >> p; | |
std::cout << "\n<------------- START ------------->\n"; | |
for (int i = 0; i <= 10; ++i) { | |
double n = start, temp = start; | |
string str = ""; | |
uint_fast64_t tempP = p; | |
while ( tempP-- ) { | |
uint64_t t = 0; | |
while ( powf(b, t) < n ) t++; | |
if ( powf(b, t) == n ) { | |
str = "1"; | |
break; | |
} | |
--t; | |
if ( str.empty() ) str += to_string(t) + "."; | |
else str += to_string(t); | |
n /= powf(b, t); | |
n = powf(n, b); | |
} | |
std::cout << "\nNew method value: " << str << " obtained via new method, using log10 from math.h: " << log10(temp) << " for the number " << start <<"\n"; | |
start = start + 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment