//-----------------------------------------------------------------------------
// Copyright © 2003 - Philip Howard - All rights reserved
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//-----------------------------------------------------------------------------
// package	libh/string
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	libh at ipal dot org
// homepage	http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 120 columns wide.
//-----------------------------------------------------------------------------

#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#include <libh/string.h>


//-----------------------------------------------------------------------------
// program	numtest
//
// purpose	Test the str_app_ld_cet function.
//
// syntax	numtest  first step last
//-----------------------------------------------------------------------------
int
main (
    int		argc
    ,
    char * *	argv
    )
{
    long double		first	;
    long double		step	;
    long double		last	;
    long double		value	;
    long double		again	;
    int			ch	;

    char		space	[256]	;

    if ( argc <= 3 ) {
	fprintf( stderr, "numencode  first step last\n" );
	return 1;
    }

#if 1
    first = str_expr_to_ld( argv[1], NULL );
    step  = str_expr_to_ld( argv[2], NULL );
    last  = str_expr_to_ld( argv[3], NULL );
#elif 1
    first = str_to_ld( argv[1], NULL );
    step  = str_to_ld( argv[2], NULL );
    last  = str_to_ld( argv[3], NULL );
#else
    sscanf( argv[1], "%La", & first );
    sscanf( argv[2], "%La", & step );
    sscanf( argv[3], "%La", & last );
#endif

    if ( step < 0.0 ) step = - step;

    printf( "from %32.24Lf  (%21.15La)\n", first, first );
    printf( "step %32.24Lf  (%21.15La)\n", step, step );
    printf( "last %32.24Lf  (%21.15La)\n", last, last );

    if ( first < last ) {
	printf( "forward\n" );
	for ( value = first; value <= last; value += step ) {
	    space[0] = 0;
	    str_app_ld_cet( space, 256, value );
	    again = str_to_ld( space, NULL );
	    if ( value < again ) ch = '<';
	    else if ( value > again ) ch = '>';
	    else ch = '=';
	    printf( "%32.24Lf -> %30s %c %32.24Lf\n", value, space, ch, again );
	}
    } else if ( first > last ) {
	printf( "backward\n" );
	for ( value = first; value >= last; value -= step ) {
	    space[0] = 0;
	    str_app_ld_cet( space, 256, value );
	    again = str_to_ld( space, NULL );
	    if ( value < again ) ch = '<';
	    else if ( value > again ) ch = '>';
	    else ch = '=';
	    printf( "%32.24Lf -> %30s %c %32.24Lf\n", value, space, ch, again );
	}
    } else {
	printf( "equal\n" );
    }

    return 0;
}

