Changeset d34f24 in git for factory/canonicalform.cc


Ignore:
Timestamp:
Jul 31, 1997, 3:10:10 PM (27 years ago)
Author:
Jens Schmidt <schmidt@…>
Branches:
(u'spielwiese', 'fe61d9c35bf7c61f2b6cbf1b56e25e2f08d536cc')
Children:
19f74feb1a8fde7c5b3782b0cea34f4990189986
Parents:
ae5205abe0fd692a06f68fb3c35cad2f62c5ce46
Message:
	* canonicalform.cc (sqrt): immediate case completely rewritten


git-svn-id: file:///usr/local/Singular/svn/trunk@587 2c84dea3-7e68-4137-9b89-c4e89433aadc
File:
1 edited

Legend:

Unmodified
Added
Removed
  • factory/canonicalform.cc

    rae5205 rd34f24  
    11/* emacs edit mode for this file is -*- C++ -*- */
    2 /* $Id: canonicalform.cc,v 1.12 1997-07-30 15:06:33 schmidt Exp $ */
     2/* $Id: canonicalform.cc,v 1.13 1997-07-31 13:10:10 schmidt Exp $ */
    33
    44#include <config.h>
     
    12441244}
    12451245
    1246 CanonicalForm
    1247 CanonicalForm::sqrt ( ) const
     1246//{{{ CanonicalForm CanonicalForm::sqrt() const
     1247//{{{ docu
     1248//
     1249// sqrt() - calculate integer square root.
     1250//
     1251// CO has to be an integer greater or equal zero.  Returns the
     1252// largest integer less or equal sqrt(CO).
     1253//
     1254// In the immediate case, we use the newton method to find the
     1255// root.  The algorithm is from H. Cohen - A Course in
     1256// Computational Algebraic Number Theory, ch. 1.7.1.
     1257//
     1258//}}}
     1259CanonicalForm
     1260CanonicalForm::sqrt() const
    12481261{
    12491262    if ( is_imm( value ) ) {
    1250         ASSERT( is_imm( value ) == INTMARK, "not implemented" );
    1251         int a = imm2int( value );
    1252         ASSERT( a >= 0, "arg to sqrt less than zero" );
    1253         if ( a == 0 || a == 1 )
    1254             return CanonicalForm( CFFactory::basic( a ) );
    1255         else {
    1256             int x0, x1 = a;
    1257             long long int h;
     1263        ASSERT( is_imm( value ) == INTMARK, "sqrt() not implemented" );
     1264        int n = imm2int( value );
     1265        ASSERT( n >= 0, "arg to sqrt() less than zero" );
     1266        if ( n == 0 || n == 1 )
     1267            return CanonicalForm( CFFactory::basic( n ) );
     1268        else {
     1269            int x, y = n;
    12581270            do {
    1259                 x0 = x1;
    1260                 h = (long long int)x0 * x0 + a - 1;
    1261                 if ( h % (2 * x0) == 0 )
    1262                     x1 = h / (2 * x0);
    1263                 else
    1264                     x1 = (h - 1)  / (2 * x0);
    1265             } while ( x1 < x0 );
    1266             return CanonicalForm( CFFactory::basic( x1 ) );
     1271                x = y;
     1272                // the intermediate result may not fit into an
     1273                // integer, but the result does
     1274                y = (unsigned int)(x + n/x)/2;
     1275            } while ( y < x );
     1276            return CanonicalForm( CFFactory::basic( x ) );
    12671277        }
    12681278    }
     
    12701280        return CanonicalForm( value->sqrt() );
    12711281}
    1272 
    1273 //{{{ int CanonicalForm::ilog2( ) const
     1282//}}}
     1283
     1284//{{{ int CanonicalForm::ilog2 () const
    12741285//{{{ docu
    12751286//
    12761287// ilog2() - integer logarithm to base 2.
    12771288//
    1278 // Returns the largest integer smaller than logarithm of CO to
    1279 // base 2.  CO should be an integer.
     1289// Returns the largest integer less or equal logarithm of CO to
     1290// base 2.  CO should be a positive integer.
    12801291//
    12811292//}}}
    12821293int
    1283 CanonicalForm::ilog2( ) const
     1294CanonicalForm::ilog2 () const
    12841295{
    12851296    if ( is_imm( value ) ) {
    1286         ASSERT( is_imm( value ) == INTMARK, "not implemented" );
     1297        ASSERT( is_imm( value ) == INTMARK, "ilog2() not implemented" );
    12871298        int a = imm2int( value );
    1288         ASSERT( a > 0, "log arg <= 0" );
     1299        ASSERT( a > 0, "arg to ilog2() less or equal zero" );
    12891300        int n = -1;
    12901301        while ( a != 0 ) {
Note: See TracChangeset for help on using the changeset viewer.