I want to create a method that only takes objects of the class it is is defined in, manipulates it and returns a new object of the same class. For example: Does anyone have an idea how to get this done? best regards, David Answer You can do something like this : You can add type checking directly in the
Tag: fractions
Rounding a rational number to the nearest integer, with half-up
Given a rational number, I need to get the nearest integer, with exact halves rounded up (that is, towards positive infinity). The results must be exact, which rules out float and Decimal, both of which have finite precision. The following code works perfectly in all cases except for negative numbers: For instance, it succeeds on: (5, 3) → 2 (4,