/*
 * [TestLog.java]
 *
 * Summary: demonstrate logarithms in Java.
 *
 * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2009-07-09 initial version
 */
package com.mindprod.example;

import static java.lang.System.*;

/**
 * demonstrate logarithms in Java.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2009-07-09 initial version
 * @since 2009-07-09
 */
final class TestLog
    {
    /**
     * Calculate base 10 logarithm
     *
     * @param x value to take log of
     *
     * @return base 10 logarithm.
     */
    private static double log10( double x )
        {
        // Math.log is base e, natural log, ln
        return Math.log( x ) / Math.log( 10 );
        }

    /**
     * Calculate base 2 logarithm
     *
     * @param x value to take log of
     *
     * @return base 2 logarithm.
     */
    private static double log2( double x )
        {
        // Math.log is base e, natural log, ln
        return Math.log( x ) / Math.log( 2 );
        }

    /**
     * Demonstrate use of logarithms
     *
     * @param args command line arguments are ignored.
     */
    @SuppressWarnings( { "UnusedParameters" } )
    public static void main( String args[] )
        {
        out.println( "natural log" );
        // Math.log is base e, natural log, ln
        out.println( Math.log( 1.0d ) );   // --> 0.0
        out.println( Math.log( Math.E ) ); // --> 1.0
        out.println( "log10" );
        // Roll your own base 10 logs, useful to compute width in digits.
        out.println( log10( 1000 ) );  // --> 2.9999999999999996
        out.println( log10( 10000 ) ); // --> 4.0
        out.println( log10( 5000 ) );  // --> 3.6989700043360187
        out.println( "Sun log10" );
        // Sun's strictmath log10, useful to compute width in digits.
        out.println( Math.log10( 1000 ) );  // --> 3.0 more accurate
        out.println( Math.log10( 10000 ) ); // --> 4.0
        out.println( Math.log10( 5000 ) );  // --> 3.6989700043360187
        out.println( "log2" );
        // Roll your own base 2 logs, useful to compute width in bits.
        out.println( log2( 8 ) );  // --> 3.0
        out.println( log2( 32 ) ); // --> 5.0
        out.println( log2( 10 ) ); // --> 3.3219280948873626
        out.println( "log1p" );
        // More accurate way to compute logs of values near 1
        // Sun describes this as log1p(x+1). You get the log of x plus 1 when you pass x as a parm.
        // In other words, you pass the difference of x relative to 1, or you pass x - 1 to get the log of x.
        out.println( Math.log( 1.001 ) );  // --> 9.995003330834232E-4
        // .001 + 1 == 1.001
        out.println( Math.log1p( .001 ) ); // --> 9.995003330835331E-4 (more accurate)
        out.println( Math.log( .999 ) );   // --> -0.0010005003335835344
        // -.001 + 1 == .999
        out.println( Math.log1p( -.001 ) ); // --> -0.0010005003335835335 (more accurate )
        }
    }