arsd.gr

Math library providing constants and functions for working with the golden ratio.

φ = a + b a = a b

import arsd.gr;

void main() {
	import std.conv;
	import std.string;
	import std.stdio;

	// `arsd.gr.goldenRatio` is a compile-time constant
	// holding the value representing the golden ratio.
	float gr = goldenRatio;
	writeln(i"The golden ratio is $(gr).");

	while (true) {
		// Prompt user to enter a length.
		write("\nEnter combined length: ");
		string rawUserInput = readln().chomp();

		// Parse user input string into a numeric floating-point value.
		float combinedLength;
		try {
			combinedLength = rawUserInput.to!float;
		}
		catch (Exception) {
			// Print error message and continue with another prompt.
			writeln("Bad input.");
			continue;
		}

		// Construct a GoldenRatioLengths structure.
		GoldenRatioLengths!float lengths = goldenRatioLengthsFromAB(combinedLength);

		// Calculate and print lengths.
		writeln(i"AB: $(lengths.ab)");
		writeln(i"a:  $(lengths.a)");
		writeln(i"b:  $(lengths.b)");
	}
}

Command-line calculator app

To build the command-line app for the golden ratio calculator, run one of the following commands.

# Run it directly from your favorite compiler.
dmd     -version=ArsdGrCalculatorMain -run gr.d -- <args>
ldc2 --d-version=ArsdGrCalculatorMain -run gr.d -- <args>

# Using rdmd works, too.
rdmd    -version=ArsdGrCalculatorMain      gr.d -- <args>

# Run it with DUB, either by fetching it from the registry or directly from its folder.
dub run --config=calculator  arsd-official:gr   -- <args>
dub run --config=calculator               :gr   -- <args>

# Or just execute this very file. (POSIX only)
./gr.d  <args>

A short help text with usage instructions can be printed by running the command-line app with argument --help.

Terminology

In mathematics, the golden ratio — usually denoted by Greek lowercase letter phi φ — is the partitioning ratio of a line segment in two subsegments that share the same ratio to each other as the combined line segment to the longer subsegment.

φ = a + b a = a b

Variable declarations
SymbolMeaning
φGolden ratio
ABA combined line segment whose length is the sum of its two subsegments in the golden ratio.
aLonger subsegment of the combined line segment AB in the golden ratio.
bShorter subsegment of combined line segment AB in the golden ratio.

The golden ratio can be calculated using the positive solution of following formula:

φ = φ 2 - 1

φ = 1 + 5 2 = 1.61803

From this it follows that for a line segment AB the longer subsegment a can be calculated with this formula:

a ( AB ) = AB 2 ( 5 - 1 ) = AB × 0.61803

The shorter subsegment b of a line segment AB is therefore calculated as follows:

b ( AB ) = AB 2 ( 3 - 5 ) = AB × 0.38196

Members

Functions

goldenRatioLengthsFromA
GoldenRatioLengths!Float goldenRatioLengthsFromA(Float value)

Constructs a GoldenRatioLengths structure from the provided value for a.

goldenRatioLengthsFromAB
GoldenRatioLengths!Float goldenRatioLengthsFromAB(Float value)

Constructs a GoldenRatioLengths structure from the provided value for AB.

goldenRatioLengthsFromB
GoldenRatioLengths!Float goldenRatioLengthsFromB(Float value)

Constructs a GoldenRatioLengths structure from the provided value for b.

main
int main(string[] args)

This is the optional library-provided main() function for the built-in calculator app.

runGoldenRatioCalculatorApp
int runGoldenRatioCalculatorApp(string[] args, std.stdio.File stdin, std.stdio.File stdout, std.stdio.File stderr)

Portable entrypoint of the command-line calculator app.

Manifest constants

goldenRatio
enum goldenRatio;

The golden ratio.

Structs

GoldenRatioLengths
struct GoldenRatioLengths(Float)

Golden ratio line segment lengths.

Suggestion Box / Bug Report