Click here to Skip to main content
15,914,444 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: How hard would it be to have a Rational Number type, so that there would never be floating point errors? Pin
kalberts10-Jul-18 4:12
kalberts10-Jul-18 4:12 
GeneralRe: How hard would it be to have a Rational Number type, so that there would never be floating point errors? Pin
swampwiz10-Jul-18 4:47
swampwiz10-Jul-18 4:47 
GeneralRe: How hard would it be to have a Rational Number type, so that there would never be floating point errors? Pin
Kirk 1038982110-Jul-18 3:50
Kirk 1038982110-Jul-18 3:50 
GeneralRe: How hard would it be to have a Rational Number type, so that there would never be floating point errors? Pin
kalberts10-Jul-18 4:24
kalberts10-Jul-18 4:24 
GeneralRe: How hard would it be to have a Rational Number type, so that there would never be floating point errors? Pin
englebart10-Jul-18 4:03
professionalenglebart10-Jul-18 4:03 
GeneralRe: How hard would it be to have a Rational Number type, so that there would never be floating point errors? Pin
Bruce Patin10-Jul-18 4:16
Bruce Patin10-Jul-18 4:16 
GeneralRe: How hard would it be to have a Rational Number type, so that there would never be floating point errors? Pin
Daniel Wilianto10-Jul-18 16:40
Daniel Wilianto10-Jul-18 16:40 
GeneralRe: How hard would it be to have a Rational Number type, so that there would never be floating point errors? Pin
kalberts11-Jul-18 9:05
kalberts11-Jul-18 9:05 
I wouldn't hesitate for a second to give that as the exercise of the week to second year programming students, implmenting the four basic arithmetic operations for rationals.

There is only a small problem: If the denominators are not identical in addition/subtraction, you can trivially find a common denominator by multiplying the two, but in the genral case, it is not the least common denominator. If you use 64 bit integers for the denominator you might delay handling this until the results are to be used (at least for small student level applications), but at some stage you will have to factorize the numerator and denominator, and remove common factors. In principle this is not difficult as long as the problem is of "student size", but Eratosthenes' sieve is not very efficient (certainly not in space!) - you wouldn't want to employ that for every addition or subtraction!

Factors of 2 are easy to get rid of: While both (numerator AND 1) and (denominator ANd 1) are nonzero, shift both right by one bit. This you can do for every basic arithmetic operation (or complex expression). Other factors are worse. If you find it acceptable to do a whole series of division/remainder operations (trying with all prime factors up to the square root of the smaller of numerator and denominator), then the space required is roughly limited to a single read-only table of known primes, but you'll certainly be keeping the division circuitry of the CPU hot Smile | :) .

My recommendation would be to delay the factorization until it is needed for presentation or conversion to other formats. Viewing/interpreting the values through a debugger would be a next to impossible, and you should implement an exception handler removing common factors if an operation would cause the numerator/denominator to overflow the integer format - maybe doing a 2-factor removal as a fast first try, and going on to a series of division operations only if the values are still above a certain overflow risk threshold. If you use 64 bit ints, you might in the worst case have to do divisions for every prime less than 4G; that is quite some operation...
GeneralRe: How hard would it be to have a Rational Number type, so that there would never be floating point errors? Pin
Vivi Chellappa11-Jul-18 14:00
professionalVivi Chellappa11-Jul-18 14:00 
GeneralRe: How hard would it be to have a Rational Number type, so that there would never be floating point errors? Pin
kalberts12-Jul-18 1:59
kalberts12-Jul-18 1:59 
GeneralRe: How hard would it be to have a Rational Number type, so that there would never be floating point errors? Pin
Vivi Chellappa12-Jul-18 23:54
professionalVivi Chellappa12-Jul-18 23:54 
GeneralRe: How hard would it be to have a Rational Number type, so that there would never be floating point errors? Pin
bjongejan18-Jul-18 0:18
bjongejan18-Jul-18 0:18 
GeneralHooray Pin
theoldfool9-Jul-18 5:04
professionaltheoldfool9-Jul-18 5:04 
GeneralRe: Hooray Pin
OriginalGriff9-Jul-18 5:11
mveOriginalGriff9-Jul-18 5:11 
GeneralRe: Hooray Pin
DRHuff9-Jul-18 5:27
DRHuff9-Jul-18 5:27 
GeneralRe: Hooray Pin
theoldfool9-Jul-18 5:33
professionaltheoldfool9-Jul-18 5:33 
GeneralRe: Hooray Pin
Bassam Abdul-Baki9-Jul-18 5:51
professionalBassam Abdul-Baki9-Jul-18 5:51 
GeneralRe: Hooray Pin
Mike Hankey9-Jul-18 6:05
mveMike Hankey9-Jul-18 6:05 
GeneralRe: Hooray Pin
DRHuff9-Jul-18 7:31
DRHuff9-Jul-18 7:31 
GeneralRe: Hooray Pin
kmoorevs9-Jul-18 6:38
kmoorevs9-Jul-18 6:38 
GeneralRe: Hooray Pin
Mycroft Holmes9-Jul-18 14:31
professionalMycroft Holmes9-Jul-18 14:31 
GeneralRe: Hooray Pin
theoldfool9-Jul-18 14:40
professionaltheoldfool9-Jul-18 14:40 
GeneralHmm, A half hour to waste before I can go home... Pin
glennPattonWork39-Jul-18 4:29
professionalglennPattonWork39-Jul-18 4:29 
GeneralCCC 9/7- SOLVED Pin
PeejayAdams9-Jul-18 0:33
PeejayAdams9-Jul-18 0:33 
GeneralRe: CCC 9/7 Pin
OriginalGriff9-Jul-18 0:48
mveOriginalGriff9-Jul-18 0:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.