log() : logarithm of x to the base e

#include <stdio.h>
int main() {
printf("\nNumber = %.2f", log(3.54)); // Number = 1.26
printf("\nNumber = %.2f", log(-3.12)); // Number = -nan
printf("\nNumber = %.2f", log(10)); // Number = 2.30
printf("\nNumber = %.2f", log(-1));// Number = -nan
printf("\nNumber = %.2f", log(0));// Number = -inf
return 0;
}
We will get log value of any number with base e by using log() function.
log(x) 
x = input number

Example

#include <stdio.h>
#include <math.h>
int main(void){
     double  x,ret;
     x=3.54;
     ret=log(x);
     printf("log(%.2f)=%.2f", x,ret);
     return 0;
}
The above code will give this output.
log(3.54)= 1.26

Example: Calculating Logarithm with Any Base

Shows how to compute logarithms with different bases using the change of base formula.
#include <stdio.h>
#include <math.h>

double log_base(double x, double base) {
    return log(x) / log(base);
}

int main() {
    double num = 8;
    double base = 2;
    printf("Log base %.1f of %.1f is %.5f\n", base, num, log_base(num, base));
    return 0;
}
Output
Log base 2.0 of 8.0 is 3.00000

Example: Handling Edge Cases in Logarithm Calculation

Highlights how the log() function behaves with special inputs like 0 and negative numbers.
#include <stdio.h>
#include <math.h>

int main() {
    double num1 = 0;
    double num2 = -5;
    double num3 = 1000;

    printf("log(0): %f\n", log(num1));  // Should return negative infinity (-inf)
    printf("log(-5): %f\n", log(num2));  // Should return NaN (Not a Number)
    printf("log(1000): %f\n", log(num3));  // Should return a valid positive value
    return 0;
}
Output
log(0): -inf
log(-5): -nan
log(1000): 6.907755


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer