<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://moddingwiki.shikadi.net/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mwin</id>
	<title>ModdingWiki - User contributions [en-gb]</title>
	<link rel="self" type="application/atom+xml" href="https://moddingwiki.shikadi.net/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mwin"/>
	<link rel="alternate" type="text/html" href="https://moddingwiki.shikadi.net/wiki/Special:Contributions/Mwin"/>
	<updated>2026-09-21T00:35:07Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.11</generator>
	<entry>
		<id>https://moddingwiki.shikadi.net/w/index.php?title=Turbo_Pascal_Real&amp;diff=10846</id>
		<title>Turbo Pascal Real</title>
		<link rel="alternate" type="text/html" href="https://moddingwiki.shikadi.net/w/index.php?title=Turbo_Pascal_Real&amp;diff=10846"/>
		<updated>2022-12-27T23:44:49Z</updated>

		<summary type="html">&lt;p&gt;Mwin: /* C# */ The previous sign check does not work (result never becomes 1).&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;While most languages use a 32-bit or 64-bit floating point decimal variable, usually called &#039;&#039;single&#039;&#039; or &#039;&#039;double&#039;&#039;, Turbo Pascal featured an uncommon 48-bit float called a &#039;&#039;real&#039;&#039; which served the same function as a float. Real variable types were frequently used in the early versions of Pascal because it did not have native support for 32-bit integers until later versions, so, if you wanted to store a number greater than 32,767 or a number with a decimal value, you had to use a real. 32 and 64-bit floats were introduced in Turbo Pascal version 5. Pascal&#039;s successor, Delphi, did not feature reals as a variable type.&lt;br /&gt;
&lt;br /&gt;
A Pascal real has a value range of 2.9 x 10&amp;lt;sup&amp;gt;-39&amp;lt;/sup&amp;gt; to 1.7 x 10&amp;lt;sup&amp;gt;38&amp;lt;/sup&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The structure of a Pascal real is seen in the diagram below.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; |&lt;br /&gt;
! Byte !! 0 !! 1 !! 2 !! 3 !! 4 !! 5&lt;br /&gt;
|-&lt;br /&gt;
! Bit !! &amp;lt;span style=&amp;quot;font-family:monospace; font-size:12pt;&amp;quot;&amp;gt;01234567&amp;lt;/span&amp;gt; !! &amp;lt;span style=&amp;quot;font-family:monospace; font-size:12pt;&amp;quot;&amp;gt;01234567&amp;lt;/span&amp;gt; !! &amp;lt;span style=&amp;quot;font-family:monospace; font-size:12pt;&amp;quot;&amp;gt;01234567&amp;lt;/span&amp;gt; !! &amp;lt;span style=&amp;quot;font-family:monospace; font-size:12pt;&amp;quot;&amp;gt;01234567&amp;lt;/span&amp;gt; !! &amp;lt;span style=&amp;quot;font-family:monospace; font-size:12pt;&amp;quot;&amp;gt;01234567&amp;lt;/span&amp;gt; !! &amp;lt;span style=&amp;quot;font-family:monospace; font-size:12pt;&amp;quot;&amp;gt;01234567&amp;lt;/span&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Value&#039;&#039;&#039; || &amp;lt;span style=&amp;quot;font-family:monospace; font-size:12pt; color:#FF0000;&amp;quot;&amp;gt;EEEEEEEE&amp;lt;/span&amp;gt; || &amp;lt;span style=&amp;quot;font-family:monospace; font-size:12pt;&amp;quot;&amp;gt;MMMMMMMM&amp;lt;/span&amp;gt; || &amp;lt;span style=&amp;quot;font-family:monospace; font-size:12pt;&amp;quot;&amp;gt;MMMMMMMM&amp;lt;/span&amp;gt; || &amp;lt;span style=&amp;quot;font-family:monospace; font-size:12pt;&amp;quot;&amp;gt;MMMMMMMM&amp;lt;/span&amp;gt; || &amp;lt;span style=&amp;quot;font-family:monospace; font-size:12pt;&amp;quot;&amp;gt;MMMMMMMM&amp;lt;/span&amp;gt; || &amp;lt;span style=&amp;quot;font-family:monospace; font-size:12pt;&amp;quot;&amp;gt;&amp;lt;span style=&amp;quot;color:#2060FF;&amp;quot;&amp;gt;S&amp;lt;/span&amp;gt;MMMMMMM&amp;lt;/span&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
Where &amp;lt;span style=&amp;quot;color:#FF0000;&amp;quot;&amp;gt;E&amp;lt;/span&amp;gt; is Exponent, M is Mantissa, and &amp;lt;span style=&amp;quot;color:#2060FF;&amp;quot;&amp;gt;S&amp;lt;/span&amp;gt; is the Sign bit.&lt;br /&gt;
&lt;br /&gt;
==Conversion==&lt;br /&gt;
Any games written in Turbo Pascal may feature game data files with reals encoded into them. However, unless your mod program is written in Turbo Pascal, you won&#039;t have any way to directly read or write these values. The simplest way to do this is to convert each real into a double when reading them from a file, and from a double into a real when writing them back to a file.&lt;br /&gt;
&lt;br /&gt;
===Real to Double===&lt;br /&gt;
This will take a real as input and give a double as output.&lt;br /&gt;
====C#====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
// This program expects a byte array named real48[6] to be loaded with the 6 bytes of the real from the file.&lt;br /&gt;
&lt;br /&gt;
Double exponentbase = 129d;&lt;br /&gt;
Double exponent = real48[0] - exponentbase; // The exponent is offset so deduct the base.&lt;br /&gt;
&lt;br /&gt;
// Now Calculate the mantissa&lt;br /&gt;
Double mantissa = 0.0;&lt;br /&gt;
Double value = 1.0;&lt;br /&gt;
// For Each Byte.&lt;br /&gt;
for (int i = 5; i &amp;gt;= 1; i--)&lt;br /&gt;
{&lt;br /&gt;
    int startbit = 7;&lt;br /&gt;
    if (i == 5)&lt;br /&gt;
    { startbit = 6; } //skip the sign bit.&lt;br /&gt;
&lt;br /&gt;
    //For Each Bit&lt;br /&gt;
    for (int j = startbit; j &amp;gt;= 0; j--)&lt;br /&gt;
    {&lt;br /&gt;
        value = value / 2;// Each bit is worth half the next bit but we&#039;re going backwards.&lt;br /&gt;
        if (((real48[i] &amp;gt;&amp;gt; j) &amp;amp; 1) == 1) //if this bit is set.&lt;br /&gt;
        {&lt;br /&gt;
            mantissa += value; // add the value.&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if (mantissa == 1.0 &amp;amp;&amp;amp; real48[0] == 0) // Test for null value&lt;br /&gt;
    return 0.0;&lt;br /&gt;
&lt;br /&gt;
double factor = ((real48[5] &amp;amp; 0x80) &amp;gt;&amp;gt; 7 == 1) ? -1 : 1; // Change sign if bit 7 is set&lt;br /&gt;
&lt;br /&gt;
return factor * (1 + mantissa) * Math.Pow(2.0, exponent);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====FreeBASIC====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;freebasic&amp;quot;&amp;gt;&lt;br /&gt;
&#039; Load the real from a file into a byte array.&lt;br /&gt;
Open &amp;quot;Real.bin&amp;quot; For Binary As #1&lt;br /&gt;
Dim Real48(0 To 5) As UByte&lt;br /&gt;
Dim ByteNo As Byte&lt;br /&gt;
For ByteNo = 0 To 5&lt;br /&gt;
    Get #1, , Real48(ByteNo)&lt;br /&gt;
Next ByteNo&lt;br /&gt;
Close #1&lt;br /&gt;
&lt;br /&gt;
&#039; Get the exponent value, and eliminate the 129 offset.&lt;br /&gt;
Dim Exponent As Double&lt;br /&gt;
Exponent = Real48(0) - 129.0&lt;br /&gt;
&lt;br /&gt;
&#039; Calculate the mantissa.&lt;br /&gt;
Dim Mantissa As Double&lt;br /&gt;
Dim Value As Double&lt;br /&gt;
Value = 1&lt;br /&gt;
For ByteNo = 5 To 1 Step -1&lt;br /&gt;
    Dim StartBit As Byte&lt;br /&gt;
    StartBit = 7&lt;br /&gt;
    If ByteNo = 5 Then StartBit = 6     &#039; Skip the sign bit.&lt;br /&gt;
    &lt;br /&gt;
    Dim BitNo As Byte&lt;br /&gt;
    For BitNo = StartBit To 0 Step -1&lt;br /&gt;
        Value = Value / 2&lt;br /&gt;
        If ((Real48(ByteNo) Shr BitNo) And 1) = 1 Then&lt;br /&gt;
            Mantissa = Mantissa + Value&lt;br /&gt;
        End If&lt;br /&gt;
    Next BitNo&lt;br /&gt;
Next ByteNo&lt;br /&gt;
&#039; Add the implicit 1.&lt;br /&gt;
Mantissa = Mantissa + 1&lt;br /&gt;
&lt;br /&gt;
&#039; Test for a null value.&lt;br /&gt;
Dim Result As Double&lt;br /&gt;
If Mantissa = 1.0 And Real48(0) = 0 Then&lt;br /&gt;
    Result = 0.0&lt;br /&gt;
Else&lt;br /&gt;
    &#039; If the sign bit is set, make the value negative.&lt;br /&gt;
    If Real48(5) AND &amp;amp;H80 = -1 Then&lt;br /&gt;
        Mantissa = -Mantissa&lt;br /&gt;
    End If&lt;br /&gt;
    &lt;br /&gt;
    &#039; Raise the mantissa to the specified exponent.&lt;br /&gt;
    Result = Mantissa * (2 ^ Exponent)&lt;br /&gt;
End If&lt;br /&gt;
&lt;br /&gt;
Print Result&lt;br /&gt;
Sleep&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Double to Real===&lt;br /&gt;
This will take a double as input and give a real as output.&lt;/div&gt;</summary>
		<author><name>Mwin</name></author>
	</entry>
</feed>