Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Catalog · How the catalog works
Anything · Lazarus or Delphi
Random projectRandom project

LCDSmartie-plugin-en

This plugin for LCDSmartie will convert the passed in value, consisting of a mantissa and an optional scale factor, to an output string formatted using engineering notation.

View on GitHub

Member rating

No ratings yet.

Sign in to rate this project.

README

Overview

In general, Engineering Notation is a special form of scientific notation in which the exponent part, having a base of 10, is raised to an integer power that is evenly divisible by 3, for example, $10^3$, $10^6$, $10^9$, etc. With this constraint in place, these exponent parts are then replaced by single letters representing their corresponding metric prefixes as a shorthand notation. For example, the value $5.32 x 10^4$, which is in scientific notation, can first be written as $53.2 x 10^3$. Then, since the metric prefix for $10^3$ is kilo, abbreviated as "k", the value can ultimately be represented as 53.2k in engineering notation.

This plugin for LCD Smartie will format a floating-point value so that it is in Engineering Notation, with the commonly known single letter representations of the applied exponent used as a suffix, when appropriate. To represent an exponent part of $10^0$, which has no single letter representation, a blank space is output instead to preserve consistency. Two modes are supported:

Base = 1000

In this default mode, the exponent part is the same "power of 10 raised to an integer that is evenly divisible by 3" multiplier that is described above. A more concise and mathematical way of depicting this that gets rid of the "evenly divisible by 3" part is to say that it fits the form $10^{3N}$, where N is any integer (in this DLL, N must be between -6 and +5). Since $10^{3N}$ is mathematically the same as $1000^{N}$, then this mode will be referred to as having a base of 1000 rather than 10.

Base = 1024

In this mode, which can be enabled by specifying keywords "PC", "1024", or "2" as documented below, the exponent part is defined as $1024^N$ rather than $1000^N$. This mode is useful for managing PC-centric values, in which the jump from kilo to Mega or from Mega to Giga, for example, is in multiples of 1024 rather than 1000.

Usage

The DLL for this plugin, en.dll, is available as both a 32-bit DLL and a 64-bit DLL. There is no functional difference between the two, but if you are using the 32-bit version of LCD Smartie, then you must also use the 32-bit version of this DLL. On the other hand, if you are using the 64-bit version of LCD Smartie, then you can use either the 32-bit or the 64-bit version of this DLL.

To install, just copy the appropriate en.dll file to the plugins subdirectory located in the same folder where the LCDSmartie.exe executable resides.

There is only one function available, which is function1, callable from within LCD Smartie as:

$dll(en, 1, <argument1>, <argument2>)

function1

Description:

This function will take in a value, specified as a combination of a base floating point value and an optional scale factor, compute the "best fit" exponent and corresponding mantissa for Engineering Notation, format the mantissa with an optional user-supplied floating point format spec, and, finally, append the single letter representing the exponent. Here, "best fit" means that the exponent will be the maximum for which the mantissa is greater than or equal to 1*.

*Special Case #1 for "1024" mode.

When a base of 1024 is being used, by specifying "PC", "1024", or "2" in Argument #2, an additional constraint is imposed on the output string: the mantissa is not allowed to be greater than 999. This is done purely for aesthetic reasons in this DLL so that the output can be constrained to having no more than 3 digits to the left of the decimal point at any time.

For example, in this mode, an input value of 1023M ("M" means Mega, which is $1024^2$) would normally already be in its best fit engineering notation output of 1023M. However, since this produces 4 digits to the left of the decimal point rather than 3, the mantissa is divided by 1024, and the exponent is incremented by 1, to give an output of 0.99G ("G" for Giga, which is $1024^3$).

*Special Case #2 for very small values.

If the value being represented is below 1a (1 atto), the output will just represent it as a value of 0 with no exponent letter. For example, passing in an input value of 1e-20 will result in an output of 0, formatted according to the format spec as described in Argument #2 below, and the engineering notation letter will be a blank space.

Argument #1

This argument contains the floating-point value optionally followed by a single engineering notation character that defines a scale factor to apply to it, using the engineering notation letters for the metric prefixes shown in the list below.

Argument #2

This serves 2 purposes:

  1. The text to the left of any colon (':') character, or the entire argument string if no colon is found, is a keyword that specifies what base value to use for the scaling exponent.
    • If the keyword is PC, 1024, or 2, then the base is set to 1024 (i.e., kilo=1024).
    • If the keyword is 1000 or 10 or is omitted altogether, then the base is set to 1000.
    • Any other text parsed here returns an error string.
  2. If argument #2 contains a colon (':') character, then all the text to the right of it is assumed to be a valid floating point format spec for the FormatFloat function in Free Pascal. If the colon does not exist, or if there is no text to the right of it, then the default format spec of ##0.00 is used.

Valid Engineering Notation Characters

The characters used for the engineering notation scale factors are shown below. These are the only valid characters that can be used in argument #1, and they are also the only ones used in the output string. In the table below, 1024 is used rather than 1000 when the base keyword of PC, 1024, or 2 is specified in argument #2.

  • P = Peta, $1000^5$
  • T = Tera, $1000^4$
  • G = Giga, $1000^3$
  • M = Mega, $1000^2$
  • k = kilo, $1000^1$
  • <blank> = $1000^0$ (i.e., no scale factor)
  • m = milli, $1000^{-1}$
  • u = micro, $1000^{-2}$
  • n = nano, $1000^{-3}$
  • p = pico, $1000^{-4}$
  • f = femto, $1000^{-5}$
  • a = atto, $1000^{-6}$

Example 1:

$dll(en,1,3200M,PC)

The value to process is $3200 * 1024^2 = 3,355,443,200$ and the output will be 3.13G

Example 2:

$dll(en,1,0.0275k,)

The value to process is $0.0275 * 1000 = 27.5$ and the output will be 27.50

Example 3:

$dll(en,1,0.0275,:##0.0000)

The value to proces is $0.0275 * 1 = 0.0275$ and the output will be 27.5000m

Compiling

If you wish to compile this source code in Lazarus, the way to control whether the resulting DLL is 32-bit or 64-bit is by setting the appropriate Build Mode in the Project Options dialog box. For a 32-bit DLL, select the "x32" build mode, and for a 64-bit DLL, select the 64-bit DLL. The output DLL has the same name, en.dll, regardless of which build mode is active, but the output directory will be different. This way, you can compile both versions without having one overwrite the other.