Code Monkey home page Code Monkey logo

fractions's Introduction

Fractions Unit

Description

The DelphiDabbler.Lib.Fractions.pas unit provides a single "Advanced" record, TFraction that encapsulates a fraction and a number of operations on it.

A whole bunch of operator overloads make it straightforward to perform all the usual arithmetic operations on a fraction and to cast a TFraction to and from an integer or floating point value.

For full details of methods and operator overloads see the comprehensive online documentation. There is no help file.

DUnit tests for TFraction are included in the download.

Compatibility

This unit requires Delphi 2009 as a minimum and has been tested on all 32 bit Windows compilers from Delphi 2009 to Delphi XE4 and 64 bit Windows compilers from Delphi XE2 to XE4.

Installation

The Fractions Unit, documentation and test suite are supplied in a zip file. Before installing you need to extract all the files from the zip file, preserving the directory structure. The following files will be extracted:

  • DelphiDabbler.Lib.Fractions.pas – Main source code.
  • README.md – The unit's read-me file.
  • MPL-2.txt – Mozilla Public License v2.0.
  • CHANGELOG.md – The project's change log.
  • Documentation.URL – Short-cut to the online documentation.

In addition to the above files you will find the DUnit tests in the Test sub-directory.

There are four possible ways to use the unit.

  1. The simplest way is to add DelphiDabbler.Lib.Fractions.pas to your projects as you need it.
  2. To make the unit easier to re-use you can either copy it to a folder on your Delphi search path, or add the folder containing the unit to the Delphi Search path. You then simply use the unit as required without needing to add it to your project.
  3. For maximum portability you can add the unit to a Delphi package. If you need help doing this see here.
  4. If you use Git you can add the ddablib/fractions GitHub repository as a Git submodule and add it to your project. Obviously, it's safer if you fork the repo and use your copy, just in case ddablib/fractions ever goes away.

Update History

A complete change log is provided in CHANGELOG.md that is included in the download.

License

The Fractions Unit (DelphiDabbler.Lib.Fractions.pas) is released under the terms of the Mozilla Public License v2.0.

All relevant trademarks are acknowledged.

Acknowledgements

  • The information on fractions in the Mathematics Help Facility at https://mathleague.com/ was useful in writing this code.
  • The GCD and LCM routines were taken from a UseNet post by Hans van Kruijssen that I found on the now defunct EFG site.
  • The DecimalToFraction routine was adapted from the Turbo Pascal code presented in "Algorithm To Convert A Decimal To A Fraction" by John Kennedy, Mathematics Department, Santa Monica College, USA.
  • lukas-hribik for reworking the LCM function to reduce the chance of integer overflow [issue #8].

Bugs and Feature Requests

Bugs can be reported or new features requested via the project's Issue Tracker. A GitHub account is required.

Please check if an issue has already been created for a similar report or request. If so then please add a comment containing as much information as you can to the existing issue, or if you've nothing to add, just add a 👍 (:+1:) comment. If there is no suitable existing issue then please add a new issue and give as much information as possible.

About the Author

I'm Peter Johnson – a hobbyist programmer living in Ceredigion in West Wales, UK, writing mainly in Delphi. My programs and other library code are available from: https://delphidabbler.com/.

This document is copyright © 2013-2023, P D Johnson.

fractions's People

Contributors

delphidabbler avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

atkins126

fractions's Issues

Add initialisation for TFraction in later Delphis

Default initialisation of TFraction leaves it in an invalid state. When declared as a field the state could be that all fields are zero, giving an invalid denominator.

Add initialisation to set numerator to 0 and denominator to 1 where initialisation of records is supported.

Add `ToString` method

Add a ToString method to convert the fraction to a string:

E.g.

TFraction.Create(3, 4).ToString renders as 3/4.

Time to release v1

This project has been in beta since 2013.

Time to test with Delphi 11 and release if OK.

Drop support for Delphi 2009 & 2010

I'm trying to rationalise ddablib to support Delphi XE and later.

Also, although this code only supports back to Delphi 2009, I can't test back before Delphi XE.

Release.bat not working

Release.bat is not packaging release zip files correctly following renaming of read-me and change log files.

Avoid integer overflow

Function LCM should be changed to

function LCM(A, B: Int64): Int64;
begin
Result := A * (B div GCD(A, B));
end;

to decrease probability of overflow

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.