Recent

Author Topic: Fractions  (Read 46281 times)

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Fractions
« Reply #30 on: March 23, 2015, 07:25:45 pm »
My demo does not call CreateFraction, see above.
« Last Edit: March 23, 2015, 07:54:13 pm by typo »

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Fractions
« Reply #31 on: March 23, 2015, 09:32:36 pm »
Well, I have changed my demo instead. Here the whole code again.

Code: [Select]
program FractionsDemo;

{$mode objfpc}{$H+}

uses
  Classes, SysUtils, Fractions;

function CalculateFraction(FirstFraction, AOperator, SecondFraction :string): string;
var
  F1, F2, Res: Fraction;
  S1, S2, S3, S4 :string;
  p :integer;
begin
  p := Pos('/', FirstFraction);
  S1 := Trim(Copy(FirstFraction, 1, p - 1));
  S2 := Trim(Copy(FirstFraction, p + 1, Length(FirstFraction)));
  p := Pos('/', SecondFraction);
  S3 := Copy(SecondFraction, 1, p - 1);
  S4 := Copy(SecondFraction, p + 1, Length(SecondFraction));
  F1 := CreateFraction(StrToInt(S1), StrToInt(S2));
  F2 := CreateFraction(StrToInt(S3), StrToInt(S4));
  case AOperator of
  '+': Res := Canonicalize(F1 + F2);
  '-': begin
         Res := Canonicalize(F1 - F2);
         if Res.den < 0 then
         begin
           Res.num := - Res.num;
           Res.den := - Res.den;
         end;
       end;
  '*': Res := Canonicalize(F1 * F2);
  '/': Res := Canonicalize(F1 / F2);
  end;
  Result := FractionToStr(Res);
end;

var
  Str1, Str2, StrOp :string;
begin
  WriteLn('Enter first fraction (a/b):');
  ReadLn(Str1);
  WriteLn('Enter operator (+, -, *, /):');
  ReadLn(StrOp);
  WriteLn('Enter second fraction (c/d):');
  ReadLn(Str2);
  WriteLn(CalculateFraction(Str1, StrOp, Str2));
  readln;
end.           

avra

  • Hero Member
  • *****
  • Posts: 2582
    • Additional info
Re: Fractions
« Reply #32 on: March 24, 2015, 10:59:28 am »
It's been a long time since I have enjoyed so much reading some topic.  ;)

+1 for all !!!    8-)

p.s. Hopefully it ends up in FPC    :D
« Last Edit: March 24, 2015, 11:01:18 am by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

goodname

  • Sr. Member
  • ****
  • Posts: 297
Re: Fractions
« Reply #33 on: March 24, 2015, 03:12:01 pm »
Just to add a little confusion but there are two more operations that may be considered. There is integer division DIV and remainder MOD.
http://wiki.freepascal.org/Div
http://wiki.freepascal.org/Mod

Some calculators do not drop the remainder during integer division. It is harder to calculate for the language but is more intuitive for humans.
7 DIV 3 = 2r1

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Fractions
« Reply #34 on: March 24, 2015, 03:45:54 pm »
Just to add a little confusion but there are two more operations that may be considered. There is integer division DIV and remainder MOD.
http://wiki.freepascal.org/Div
http://wiki.freepascal.org/Mod

Some calculators do not drop the remainder during integer division. It is harder to calculate for the language but is more intuitive for humans.
7 DIV 3 = 2r1
http://www.freepascal.org/docs-html/rtl/math/divmod.html

Bart

  • Hero Member
  • *****
  • Posts: 5674
    • Bart en Mariska's Webstek
Re: Fractions
« Reply #35 on: March 24, 2015, 05:52:39 pm »
Just to add a little confusion but there are two more operations that may be considered. There is integer division DIV and remainder MOD.

DIV and MOD are integer operations. How would they apply to fractions?
I would find it rather un-intuitive to have div/mod on fractions.
2 1/4 div 1/2 = 4?
3/4 mod 1/2 = 1/4?

Bart

goodname

  • Sr. Member
  • ****
  • Posts: 297
Re: Fractions
« Reply #36 on: March 24, 2015, 06:50:40 pm »
DIV and MOD are integer operations. How would they apply to fractions?
After giving it some thought I think your right. The DIV and MOD operations don't really apply to fractions. A conversion from mixed to rational and a simplification operation might though.
3/2 <--> 1+1/2 Conversion
3/9 --> 1/3  Simplification

Bart

  • Hero Member
  • *****
  • Posts: 5674
    • Bart en Mariska's Webstek
Re: Fractions
« Reply #37 on: March 24, 2015, 07:05:49 pm »
After giving it some thought I think your right. The DIV and MOD operations don't really apply to fractions. A conversion from mixed to rational and a simplification operation might though.
3/2 <--> 1+1/2 Conversion
3/9 --> 1/3  Simplification

See the Resolve and Normalize methods in my fractions unit:
Normalize: 3/9 -> 1/3
Resolve: 5/2 -> 2 1/2

Bart

goodname

  • Sr. Member
  • ****
  • Posts: 297
Re: Fractions
« Reply #38 on: March 24, 2015, 07:27:00 pm »
The fractions unit looks really good. Think the opposite of resolve is missing.

2 1/2 -> 5/2

I'm also wondering if Init should accept whole, numerator, and denominator arguments so that 2 1/2 would be accepted as valid.

Bart

  • Hero Member
  • *****
  • Posts: 5674
    • Bart en Mariska's Webstek
Re: Fractions
« Reply #39 on: March 24, 2015, 08:26:09 pm »
The fractions unit looks really good. Think the opposite of resolve is missing.

2 1/2 -> 5/2

Why?
Simply use ToString.

I'm also wondering if Init should accept whole, numerator, and denominator arguments so that 2 1/2 would be accepted as valid.

I removed Init method.
It's unneccessary and the compiler emits hints.
To initialize use:
Code: [Select]
  AFraction := Fraction(N,D);

The overloaded assignment (':=') operator can be made to accept such a thing.
I'll implement this.
Thanks for the suggestion.

Bart

goodname

  • Sr. Member
  • ****
  • Posts: 297
Re: Fractions
« Reply #40 on: March 24, 2015, 08:44:56 pm »
One more suggestion. I notice that strToInt is used in AFraction := Fraction(N,D); Should consider using tryStrToInt to catch invalid input.

BeniBela

  • Hero Member
  • *****
  • Posts: 955
    • homepage
Re: Fractions
« Reply #41 on: March 24, 2015, 10:26:48 pm »
DIV and MOD are integer operations. How would they apply to fractions?
After giving it some thought I think your right. The DIV and MOD operations don't really apply to fractions.

a/b DIV c/d = (a*d) div (b*c )

a/b MOD c/d = ( (a*d) div (b*c ) ) * (b*c - a*d)/(b*d)


Bart

  • Hero Member
  • *****
  • Posts: 5674
    • Bart en Mariska's Webstek
Re: Fractions
« Reply #42 on: March 24, 2015, 10:27:55 pm »
One more suggestion. I notice that strToInt is used in AFraction := Fraction(N,D); Should consider using tryStrToInt to catch invalid input.

I implemented conversion functions from String to TFraction allowing for input like '2 1/2' and using TryStrToInt inside these functions and raise EConvertErrors if it fails.
The input however is not allowed to have prepending or trailing spaces, nor more than 1 space separating the integer part from the fraction part:
'1 1/2' is valid
' 1 1/2' is invalid
'1  1/2' is invalid
'1 1/ 2' is invalid

Now you can do:
Code: [Select]
var
  F1: TFraction;
begin
  F1 := '1 2/3';
  //equivalent code:
  F1 := StrToFraction('1 2/3');
end.

See r253.

Bart
« Last Edit: March 24, 2015, 10:34:25 pm by Bart »

Bart

  • Hero Member
  • *****
  • Posts: 5674
    • Bart en Mariska's Webstek
Re: Fractions
« Reply #43 on: March 24, 2015, 10:31:06 pm »
DIV and MOD are integer operations. How would they apply to fractions?
a/b DIV c/d = (a*d) div (b*c )

The math was not the point, it's trivial.
It's just that div and mod are defined as integer operations.
Fractions are NOT integers, so div and mod do not apply (we also have no div and mod for floating point types).

Bart
« Last Edit: March 24, 2015, 11:58:18 pm by Bart »

circular

  • Hero Member
  • *****
  • Posts: 4467
    • Personal webpage
Re: Fractions
« Reply #44 on: March 24, 2015, 10:59:05 pm »
I would agree that Div cannot be applied to fractions and floating point numbers. However it could be implemented as: a div b = Trunc(a/b)

Mod can surely be implemented for floating point numbers as: a mod b = a - Trunc(a/b)*b
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018