//-----------------------------------------------------------------------------
// Copyright © 2004 - 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/arith
// 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 <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <libh/arith.h>
#include <libh/string.h>


//-----------------------------------------------------------------------------
// program	scale
//
// purpose	Rescale numbers from one linear range to another.
//
// syntax	scale new1 new2 old1 old2 number ...
//-----------------------------------------------------------------------------
int
main (
    int		argc
    ,
    char * *	argv
    ,
    char * *	envp
    )
{
    double	new1	;
    double	new2	;
    double	old1	;
    double	old2	;
    double	number	;
    int		len	;
    char	numstr	[128];

    if ( argc <= 5 ) {
	fprintf( stderr, "Syntax:  scale  new1  new2  old1  old2  number ...\n" );
	return 1;
    }

    ++ argv; -- argc; new1 = str_expr_to_d( * argv, NULL );
    ++ argv; -- argc; new2 = str_expr_to_d( * argv, NULL );
    ++ argv; -- argc; old1 = str_expr_to_d( * argv, NULL );
    ++ argv; -- argc; old2 = str_expr_to_d( * argv, NULL );

    while ( ++ argv, -- argc ) {
	number = str_expr_to_d( * argv, NULL );
	number = rescale_d( new1, new2, old1, old2, number );
	numstr[127] = 0;
	snprintf( numstr, 128, "%127.125f", number );
	len = strlen( numstr );
	while ( len >= 2 && numstr[len-1] == '0' && numstr[len-2] != '.' ) -- len;
	numstr[len] = 0;
	puts( numstr );
	fflush(stdout);
    }

    return 0;
}

