Changeset 19ee5eb in git


Ignore:
Timestamp:
Feb 1, 2021, 1:32:52 PM (2 years ago)
Author:
Hans Schoenemann <hannes@…>
Branches:
(u'jengelh-datetime', 'ceac47cbc86fe4a15902392bdbb9bd2ae0ea02c6')(u'spielwiese', 'cdfcdb8287f66bc6070028082cbbc6eff10e609b')
Children:
4218c13e2434358d0a056b78d0a40377a11bcdb1
Parents:
f60ab1f6c47a09dc3472f9d3cabd270622c46327
Message:
fix: division by 0: return 0, not NULL
Location:
libpolys/coeffs
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • libpolys/coeffs/flintcf_Qrat.cc

    rf60ab1 r19ee5eb  
    446446  const fmpq_rat_ptr y = (fmpq_rat_ptr) b;
    447447  const fmpq_ctx_ptr ctx = (fmpq_ctx_ptr) ((data_ptr)c->data)->ctx;
    448   if (fmpq_mpoly_is_zero(y->num, ctx))
    449   {
    450     WerrorS(nDivBy0);
    451     return NULL;
    452   }
    453448  fmpq_rat_ptr res = (fmpq_rat_ptr) omAlloc(sizeof(fmpq_rat_struct));
    454449  fmpq_rat_init(res, c);
     450  if (fmpq_mpoly_is_zero(y->num, ctx))
     451  {
     452    WerrorS(nDivBy0);
     453    return (number)res;
     454  }
    455455  if (fmpq_mpoly_equal(x->den, y->num, ctx)) /* denominators equal */
    456456  {
     
    531531static number ExactDiv(number a, number b, const coeffs c)
    532532{
    533   fmpq_rat_ptr res = (fmpq_rat_ptr) omAlloc(sizeof(fmpq_rat_struct));
    534533  const fmpq_rat_ptr x = (fmpq_rat_ptr) a;
    535534  const fmpq_rat_ptr y = (fmpq_rat_ptr) b;
    536535  const fmpq_ctx_ptr ctx = (fmpq_ctx_ptr) ((data_ptr)c->data)->ctx;
     536  fmpq_rat_ptr res = (fmpq_rat_ptr) omAlloc(sizeof(fmpq_rat_struct));
     537  fmpq_rat_init(res, c);
    537538  if (fmpq_mpoly_is_zero(y->num, ctx))
    538539  {
    539540     WerrorS(nDivBy0);
    540      return NULL;
    541   }
    542   fmpq_rat_init(res, c);
     541     return (number)res;
     542  }
    543543  fmpq_mpoly_div(res->num, x->num, y->num, ctx);
    544544  assume(fmpq_mpoly_is_one(x->den, ctx));
  • libpolys/coeffs/gnumpc.cc

    rf60ab1 r19ee5eb  
    142142  {
    143143    WerrorS(nDivBy0);
     144    r = new gmp_complex( 0 );
    144145  }
    145146  else
     
    194195    // a/0 = error
    195196    WerrorS(nDivBy0);
    196     return NULL;
     197    return (number)new gmp_complex( 0 );
    197198  }
    198199  gmp_complex* res = new gmp_complex( (*(gmp_complex*)a) / (*(gmp_complex*)b) );
  • libpolys/coeffs/gnumpfl.cc

    rf60ab1 r19ee5eb  
    139139  {
    140140    WerrorS(nDivBy0);
     141    f= new gmp_float( 0 );
    141142  }
    142143  else
     
    186187{
    187188  assume( getCoeffType(r) == n_long_R );
    188 
     189 
     190  gmp_float* f;
    189191  if ( ((gmp_float*)b)->isZero() )
    190192  {
    191193    // a/0 = error
    192194    WerrorS(nDivBy0);
    193     return NULL;
    194   }
    195   gmp_float* f= new gmp_float( (*(gmp_float*)a) / (*(gmp_float*)b) );
     195    f= new gmp_float( 0 );
     196  }
     197  else
     198  {
     199    f= new gmp_float( (*(gmp_float*)a) / (*(gmp_float*)b) );
     200  }
    196201  return (number)f;
    197202}
  • libpolys/coeffs/modulop.cc

    rf60ab1 r19ee5eb  
    234234  else
    235235  {
    236     if ((z==0)&&(n==0)) WerrorS(nDivBy0);
     236    if ((z==0)&&(n==0))
     237    {
     238      WerrorS(nDivBy0);
     239      *a=(number)0L;
     240    }
    237241    else
    238242    {
  • libpolys/coeffs/rintegers2.cc

    rf60ab1 r19ee5eb  
    227227}
    228228
    229 static number nrzDiv (number a,number b, const coeffs)
    230 {
    231   mpz_ptr erg = (mpz_ptr) omAllocBin(gmp_nrz_bin);
    232   mpz_init(erg);
    233   mpz_ptr r = (mpz_ptr) omAllocBin(gmp_nrz_bin);
    234   mpz_init(r);
    235   mpz_tdiv_qr(erg, r, (mpz_ptr) a, (mpz_ptr) b);
    236   //if (!nrzIsZero((number) r, R))
    237   //{
    238   //  WerrorS("Division by non divisible element.");
    239   //  WerrorS("Result is without remainder.");
    240   //}
    241   mpz_clear(r);
    242   omFreeBin(r, gmp_nrz_bin);
    243   return (number) erg;
    244 }
    245 
    246 static number nrzExactDiv (number a,number b, const coeffs)
    247 {
    248   mpz_ptr erg = (mpz_ptr) omAllocBin(gmp_nrz_bin);
    249   mpz_init(erg);
    250   mpz_tdiv_q(erg, (mpz_ptr) a, (mpz_ptr) b);
     229static number nrzDiv (number a,number b, const coeffs r)
     230{
     231  mpz_ptr erg = (mpz_ptr) omAllocBin(gmp_nrz_bin);
     232  mpz_init(erg);
     233  if (nrzIsZero(b,r))
     234  {
     235    WerrorS(nDivBy0);
     236  }
     237  else
     238  {
     239    mpz_ptr r = (mpz_ptr) omAllocBin(gmp_nrz_bin);
     240    mpz_init(r);
     241    mpz_tdiv_qr(erg, r, (mpz_ptr) a, (mpz_ptr) b);
     242    mpz_clear(r);
     243    omFreeBin(r, gmp_nrz_bin);
     244  }
     245  return (number) erg;
     246}
     247
     248static number nrzExactDiv (number a,number b, const coeffs r)
     249{
     250  mpz_ptr erg = (mpz_ptr) omAllocBin(gmp_nrz_bin);
     251  mpz_init(erg);
     252  if (nrzIsZero(b,r))
     253  {
     254    WerrorS(nDivBy0);
     255  }
     256  else
     257  {
     258    mpz_tdiv_q(erg, (mpz_ptr) a, (mpz_ptr) b);
     259  }
    251260  return (number) erg;
    252261}
     
    331340  {
    332341    WerrorS("Non invertible element.");
    333     return (number)NULL;
     342    return nrzInit(0,r);
    334343  }
    335344  return nrzCopy(c,r);
  • libpolys/coeffs/rintegers3.cc

    rf60ab1 r19ee5eb  
    744744{
    745745  assume(SR_TO_INT(b));
    746   if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
     746  if (nrzIsZero(b))
     747  {
     748    WerrorS(nDivBy0);
     749    return INT_TO_SR(0);
     750  }
     751  else if (n_Z_IS_SMALL(a) && n_Z_IS_SMALL(b))
    747752  {
    748753    //if (SR_TO_INT(a) % SR_TO_INT(b))
     
    810815  assume(SR_TO_INT(b));
    811816  mpz_t aa, bb;
    812   if (n_Z_IS_SMALL(a))
     817  if (nrzIsZero(b))
     818  {
     819    WerrorS(nDivBy0);
     820    return INT_TO_SR(0);
     821  }
     822  else if (n_Z_IS_SMALL(a))
    813823    mpz_init_set_si(aa, SR_TO_INT(a));
    814824  else
     
    857867  {
    858868    WerrorS("Non invertible element.");
    859     return (number)NULL;
     869    return nrzInit(0,r);
    860870  }
    861871  return c; // has to be 1 or -1....
  • libpolys/coeffs/rmodulo2m.cc

    rf60ab1 r19ee5eb  
    412412      }
    413413    }
    414     if ((unsigned long)b % 2 == 0)
     414    if ((long)b==0L)
     415    {
     416      WerrorS(nDivBy0);
     417      return (number)0L;
     418    }
     419    else if ((unsigned long)b % 2 == 0)
    415420    {
    416421      WerrorS("Division not possible, even by cancelling zero divisors.");
  • libpolys/coeffs/rmodulon.cc

    rf60ab1 r19ee5eb  
    248248  mpz_ptr erg = (mpz_ptr)omAllocBin(gmp_nrz_bin);
    249249  mpz_init(erg);
    250   mpz_invert(erg, (mpz_ptr)c, r->modNumber);
     250  if (nrnIsZero(c,r))
     251  {
     252    WerrorS(nDivBy0);
     253  }
     254  else
     255  {
     256    mpz_invert(erg, (mpz_ptr)c, r->modNumber);
     257  }
    251258  return (number) erg;
    252259}
     
    547554static number nrnDiv(number a, number b, const coeffs r)
    548555{
    549   if (r->is_field)
     556  if (nrnIsZero(b,r))
     557  {
     558    WerrorS(nDivBy0);
     559    return nrnInit(0,r);
     560  }
     561  else if (r->is_field)
    550562  {
    551563    number inv=nrnInvers(b,r);
     
    613625  omFreeBin(g, gmp_nrz_bin);
    614626  return (number)rr;
    615 }
    616 
    617 static number nrnIntDiv(number a, number b, const coeffs r)
    618 {
    619   mpz_ptr erg = (mpz_ptr)omAllocBin(gmp_nrz_bin);
    620   mpz_init(erg);
    621   mpz_tdiv_q(erg, (mpz_ptr)a, (mpz_ptr)b);
    622   return (number)erg;
    623627}
    624628
Note: See TracChangeset for help on using the changeset viewer.