//===========================================================================//
// File:	scalar.cpp                                                          //
// Project: MUNGA           Brick: None assigned - coding styles, etc.       //
// Contents: Base information used by all MUNGA source files                 //
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
//---------------------------------------------------------------------------//
// Copyright (C) 1994-1995, Virtual World Entertainment, Inc.					  //
//                      PROPRIETARY AND CONFIDENTIAL                         //
//===========================================================================//

#include <munga.hpp>
#pragma hdrstop

#if !defined(SCALAR_HPP)
	#include <scalar.hpp>
#endif

int
	Round(Scalar value)
{
	int whole_part = floor(value);
	Scalar fractional_part = value - whole_part;
	if (fractional_part >= 0.5)
	{
		return whole_part + 1;
	}
	else
	{
		return whole_part;
	}
}



void
	Find_Roots(
		Scalar a,		// a*x*x + b*x + c = 0
		Scalar b,
		Scalar c,
		Scalar *center,
		Scalar *range
	)
{

	//
	//---------------------------------
	// See if the quadratic is solvable
	//---------------------------------
	//
	*range = b*b - 4.0f*a*c;
	if (*range < 0.0f || Small_Enough(a))
	{
		*range = -1.0f;
	}

	else
	{
		//
		//---------------------------
		// Solve the single root case
		//---------------------------
		//
		a *= 2.0f;
		*center = -b / a;
		if (*range < SMALL)
		{
			*range = 0.0f;
		}

		//
		//--------------------------
		// Find the two-root extents
		//--------------------------
		//
		else
		{
			*range = Sqrt(*range);
			*range /= a;
		}
	}
}

static void
	Verify_Arguments(
		const char *str,
		void *value
	)
{
	if (str == NULL)
	{
		Fail("Convert_From_Ascii - str == NULL");
	}
	if (value == NULL)
	{
		Fail("Convert_From_Ascii - value == NULL");
	}
	if (isalpha(*str))
	{
		DEBUG_STREAM << "Convert_From_Ascii - str == " << str << "\n";
		Fail("Convert_From_Ascii - isalpha(*str)");
	}
}

void
	Convert_From_Ascii(
		const char *str,
      char *value
   )
{
	#if 0
		Check_Pointer(str);
		Check_Pointer(value);
		if (isalpha(*str))
		{
			Dump(str);
		}
		Verify(!isalpha(*str));
	#else
		Verify_Arguments(str, value);
	#endif
	*value = (char)atoi(str);
}

void
   Convert_From_Ascii(
      const char *str,
      unsigned char *value
   )
{
	#if 0
		Check_Pointer(str);
		Check_Pointer(value);
		if (isalpha(*str))
		{
			Dump(str);
		}
		Verify(!isalpha(*str));
	#else
		Verify_Arguments(str, value);
	#endif
	*value = (unsigned char)atoi(str);
}

void
   Convert_From_Ascii(
      const char *str,
      int *value
   )
{
	#if 0
		Check_Pointer(str);
		Check_Pointer(value);
		if (isalpha(*str))
		{
			Dump(str);
		}
		Verify(!isalpha(*str));
	#else
		Verify_Arguments(str, value);
	#endif
	*value = atoi(str);
}

void
   Convert_From_Ascii(
      const char *str,
      unsigned int *value
   )
{
	#if 0
		Check_Pointer(str);
		Check_Pointer(value);
		if (isalpha(*str))
		{
			Dump(str);
		}
		Verify(!isalpha(*str));
	#else
		Verify_Arguments(str, value);
	#endif
	*value = atoi(str);
}

void
   Convert_From_Ascii(
      const char *str,
      long *value
   )
{
	#if 0
		Check_Pointer(str);
		Check_Pointer(value);
		if (isalpha(*str))
		{
			Dump(str);
		}
		Verify(!isalpha(*str));
	#else
		Verify_Arguments(str, value);
	#endif
	*value = atol(str);
}

void
   Convert_From_Ascii(
      const char *str,
      unsigned long *value
   )
{
	#if 0
		Check_Pointer(str);
		Check_Pointer(value);
		if (isalpha(*str))
		{
			Dump(str);
		}
		Verify(!isalpha(*str));
	#else
		Verify_Arguments(str, value);
	#endif
	*value = atol(str);
}

void
   Convert_From_Ascii(
      const char *str,
      Scalar *value
   )
{
	#if 0
		Check_Pointer(str);
		Check_Pointer(value);
		if (isalpha(*str))
		{
			Dump(str);
		}
		Verify(!isalpha(*str));
	#else
		Verify_Arguments(str, value);
	#endif
   *value = atof(str);
}


