Project

General

Profile

Design #1563

BigRat: ctor from machine int

Added by John Abbott over 3 years ago. Updated over 3 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Safety
Target version:
Start date:
15 Jan 2021
Due date:
% Done:

100%

Estimated time:
1.55 h
Spent time:

Description

Bruns asks the following "why?"

  BigRat q(2);     // compilation fails, must use q(2,1)
  BigRat q; q = 2; // works fine

Why disallow a ctor from machine integer, but allow it for assignment?


Related issues

Related to CoCoALib - Design #1180: BigRat(0) unexpectedly compiles! (calls ctor with mpq_t arg)Closed2018-04-18

History

#1 Updated by John Abbott over 3 years ago

  • Related to Design #1180: BigRat(0) unexpectedly compiles! (calls ctor with mpq_t arg) added

#2 Updated by John Abbott over 3 years ago

  • Status changed from New to In Progress
  • % Done changed from 0 to 10

There were 2 reasons for prohibiting ctor from a machine int: ambiguity of BigInt(0) since 0 could also be interpreted as null point, and the risk of silly typos passing undetected e.g. BigRat Q(2/3).

According to issue #1180 the ambiguity problem has been resolved by using pseudo-ctors.
The "silly typo" is not resolvable since C++ handles the expression directly.

Bruns is quite right that we are inconsistent: if we allow BigRat q; q = 2/3; then it makes no sense to forbid BigRat(2/3).
They are equally misleading.

#3 Updated by John Abbott over 3 years ago

  • % Done changed from 10 to 20

I have just discussed with Anna. Bruns's argument is persuasive: our original decision was to protect the user against easy typos, but in practice it offers little protection (since the user can simply use assignment instead).

Anna is also convinced.

#4 Updated by John Abbott over 3 years ago

  • Status changed from In Progress to Resolved
  • Assignee set to John Abbott
  • % Done changed from 20 to 70

I have added 2 new ctors (one from MachineInt and one from BigInt).
They are explicit (as is the ctor for BigInt from a machine int).

Everything compiles, and all tests pass (even with debugging active).
Need to update documentation.

There are several places where BigRat(0,1) appears; should I replace these with BigRat(0)? Probably...

#5 Updated by John Abbott over 3 years ago

  • Status changed from Resolved to Feedback
  • % Done changed from 70 to 90
  • Estimated time set to 1.55 h

I have now changed all calls BigRat(...,1) into just BigRat(...) in examples, tests, and source code.
Checking in after all tests have passed.

#6 Updated by John Abbott over 3 years ago

  • Status changed from Feedback to Closed
  • % Done changed from 90 to 100

Also available in: Atom PDF