Trying to Calculate logarithm base 10 without Math.h (Really close) Just having problems with connecting functions

tyooo

I'm trying to learn how to calculate the logarithm base 10 of any numbers that I enter via scanf to my code. I figure that I could calculate the ln(a) a being the number input. I have a working code that calculates this; however now i just want to divide any numbers that my ln(a) code outputs by the defined LN10. This is because the natural log of a number divided by the natural log of 10 will output my required log base 10 value that I am working to achieve. Here is the mess I have at the moment. Any help is extremely appreciated!

#define _CRT_SECURE_NO_WARNINGS
#define ALMOSTZERO 0.0000000000000000001
#define LN10 2.3025850929940456840179914546844

#include <stdio.h>

double log10(double);
double ln(double);

void main()
{
    {
    double x, a;
    printf("Enter value: ");
    scanf("%lf", &x);
    while (x > 0)
    {
        double log10 = ln(x) * LN10;
        printf("log10(%lf)=%lf\n", x, a);
        printf("Enter value:  ");
        scanf("%lf", &x);
    }
    }
}

double ln(double x)
{
    double sum = 0.0;
    double xmlxpl = (x - 1) / (x + 1);
    double denom = 1.0;
    double frac = xmlxpl;
    double term = frac / denom;

    while (term > ALMOSTZERO)
    {
        sum += term;
        //generate next term
        denom += 2.0;
        frac = frac * xmlxpl * xmlxpl;
        term = frac / denom;
    }
    return 2.0 * sum;
}
Bob__

There are some issues in your code, but what you need to calculate the log10 having written the function to calculate the ln of a number is just another simple function:

#define LN10 2.3025850929940456840179914546844

double log10( double x ) {
    return ln(x) / LN10;    
}

I'd change your ln function too, at least the condition to stop the iterations, becuase term can become little enough that sum == sum + term (numerically speaking). In your actual code you can stop earlier, checking that abs(term) be less then some epsilon relative to the value of sum. I simply used this:

double ln(double x)
{
    double old_sum = 0.0;
    double xmlxpl = (x - 1) / (x + 1);
    double xmlxpl_2 = xmlxpl * xmlxpl;
    double denom = 1.0;
    double frac = xmlxpl;
    double term = frac;                 // denom start from 1.0
    double sum = term;

    while ( sum != old_sum )
    {
        old_sum = sum;
        denom += 2.0;
        frac *= xmlxpl_2;
        sum += frac / denom;
    }
    return 2.0 * sum;
}

This will save you some iterations giving the same (approximated) result of your code. To take care of the last terms you should adopt some other numeric strategy.

Your main needs some changes too. At least more control of the user input:

#include <stdio.h>

double log10(double);
double ln(double);

int main()
{
    double x, a;
    printf("This program calculates the logarithm base 10.\n");

    printf("Enter a positive value: ");
    while ( 1 == scanf("%lf", &x)  &&  x > 0.0)
    {
        double a = log10(x);
        printf("log10(%lf) = %.12lf\n", x, a);
        printf("Enter a positive value: ");
    }
    return 0;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Logarithm computing without math.h

From Dev

Without that much of math.h functions, what's a simpler way of making a logarithm function?

From Dev

Is <cmath> or <math.h> really needed? Compiles without it

From Dev

Algorithm for computing base-10 logarithm in Python

From Dev

Math application to calculate advance problems

From Dev

Converting binary to base 10 without math.pow( )?

From Dev

C++ math functions can be used without including the directive "math.h" in VS 2013

From Dev

Having problems calling functions in swift

From Dev

How to calculate natural logarithm in the Windows 10 calculator app?

From Dev

Do we really need to call flush() just before close() today?

From Dev

Calculate logarithm in python

From Dev

Having problems trying to resize gridworld grid

From Dev

Having problems trying to resize gridworld grid

From Dev

Read math functions from file and calculate

From Dev

Read math functions from file and calculate

From Dev

Beginner C programmer having problems with string functions

From Dev

Windows users having problems connecting to an Ubuntu-based SAMBA server

From Dev

Writing logarithm formulas on LibreOffice Math

From Dev

Problems trying to calculate FWHM with scipy.interpolate

From Java

NumPy: Logarithm with base n

From Dev

VBA logarithm of any base

From Dev

Logarithm to integer base of integer

From Dev

Updating a mysql production server without having problems

From Dev

Calculate logarithm function in a raster in R

From Dev

Getting OverflowError: math range error(trying to calculate power of a number)

From Dev

Getting OverflowError: math range error(trying to calculate power of a number)

From Dev

Using a base class without having an explict cast

From Dev

Jison / Flex: Trying to capture anything (.*) between two tokens but having problems

From Dev

Having problems with Kafka AdminClient trying to get the lag of a consumer

Related Related

  1. 1

    Logarithm computing without math.h

  2. 2

    Without that much of math.h functions, what's a simpler way of making a logarithm function?

  3. 3

    Is <cmath> or <math.h> really needed? Compiles without it

  4. 4

    Algorithm for computing base-10 logarithm in Python

  5. 5

    Math application to calculate advance problems

  6. 6

    Converting binary to base 10 without math.pow( )?

  7. 7

    C++ math functions can be used without including the directive "math.h" in VS 2013

  8. 8

    Having problems calling functions in swift

  9. 9

    How to calculate natural logarithm in the Windows 10 calculator app?

  10. 10

    Do we really need to call flush() just before close() today?

  11. 11

    Calculate logarithm in python

  12. 12

    Having problems trying to resize gridworld grid

  13. 13

    Having problems trying to resize gridworld grid

  14. 14

    Read math functions from file and calculate

  15. 15

    Read math functions from file and calculate

  16. 16

    Beginner C programmer having problems with string functions

  17. 17

    Windows users having problems connecting to an Ubuntu-based SAMBA server

  18. 18

    Writing logarithm formulas on LibreOffice Math

  19. 19

    Problems trying to calculate FWHM with scipy.interpolate

  20. 20

    NumPy: Logarithm with base n

  21. 21

    VBA logarithm of any base

  22. 22

    Logarithm to integer base of integer

  23. 23

    Updating a mysql production server without having problems

  24. 24

    Calculate logarithm function in a raster in R

  25. 25

    Getting OverflowError: math range error(trying to calculate power of a number)

  26. 26

    Getting OverflowError: math range error(trying to calculate power of a number)

  27. 27

    Using a base class without having an explict cast

  28. 28

    Jison / Flex: Trying to capture anything (.*) between two tokens but having problems

  29. 29

    Having problems with Kafka AdminClient trying to get the lag of a consumer

HotTag

Archive