Recent

Author Topic: Fractions  (Read 46312 times)

Bart

  • Hero Member
  • *****
  • Posts: 5677
    • Bart en Mariska's Webstek
Re: Fractions
« Reply #75 on: November 18, 2015, 04:36:36 pm »
The fractions unit has been moved to the Lazarus-CCR repository.

Thee is also a Fractions Wiki page.
Feel free to improve it.

Bart

creaothceann

  • Sr. Member
  • ****
  • Posts: 266
Re: Fractions
« Reply #76 on: June 11, 2025, 05:52:55 pm »
I'm surprised that no one has mentioned a need for inverting a fraction to get its reciprocal. I use it often to convert a frequency (x / 1 second) to the duration of one cycle of that frequency (1 / x).

Code: [Select]
{$ModeSwitch AdvancedRecords}
{$ModeSwitch TypeHelpers}

interface

type
f32 = Single;  i32 = LongInt;   i64 = Int64;   int = i32;
f64 = Double;  u32 = LongWord;  u64 = QWord;  uint = u32;

TFractionHelper = type helper for TFraction
{ procedure InterlockedInvert;  inline;}
procedure            Invert;  inline;
end;


implementation

{procedure TFractionHelper.InterlockedInvert;
begin
InterlockedExchange64(FNumerator, FDenominator);  // fails because a type helper can only access public variables
end;}


procedure TFractionHelper.Invert;  // not thread-safe
var
i : i64;
begin
tmp         := Numerator;
Numerator   := Denominator;
Denominator := i;
end;

 

TinyPortal © 2005-2018