BinaryToFloat (String function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
 
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Template:String:BinaryToFloat subtitle}}
{{Template:String:BinaryToFloat subtitle}}


This [[Intrinsic classes|intrinsic]] function converts a binary string representation of a floating point number to that number.
<var>BinaryToFloat</var> is an [[Intrinsic classes|intrinsic]] function converts a binary string representation of a floating point number to that number.


The <var>BinaryToFloat</var> function is available as of version 7.5 of the <var class=product>Sirius Mods</var>.
==Syntax==
==Syntax==
{{Template:String:BinaryToFloat syntax}}
{{Template:String:BinaryToFloat syntax}}
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%num</th>
<tr><th>%number</th>
<td>A numeric variable to receive the float value of the                                                     method object string.                                                                                         </td></tr>
<td>A numeric variable to receive the float value of the method object <var class="term">string</var>.</td></tr>
<tr><th>string</th>
<tr><th>string</th>
<td>A binary string value that can have a length of 4, 8, or 16 bytes.</td></tr>
<td>A binary string value that can have a length of 4, 8, or 16 bytes.</td></tr>
Line 15: Line 14:


==Usage notes==
==Usage notes==
*The inverse of the <var>BinaryToFloat</var> method is [[FloatToBinary (Float function)|FloatToBinary]].
<ul><li><var>BinaryToFloat</var> is available as of <var class="product">Sirius Mods</var> version 7.5.</ul>


==Examples==
==Examples==
 
The following program demonstrates the <var>BinaryToFloat</var> method, converting a series of different-length representations of the same float value.
The following program demonstrates the <var>BinaryToFloat</var> method,
converting a series of different-length representations of the same float value.
 
The example also uses:
*The [[Right (String function)|Right]] method.
*The [[Substring (String function)|Substring]] method.
*The [[PrintText statement|PrintText]] statement.
<p class="code">begin
<p class="code">begin
   image silly
   image silly
Line 37: Line 29:


   for %i from -3.3 to 3.3 by 1.1
   for %i from -3.3 to 3.3 by 1.1
       printText {%i:right(15)}:
       [[PrintText statement|printText]] {%i:[[Right (String function)|right]](15)}:
       %silly:a = %i; %silly:b = %i; %silly:c = %i;
       %silly:a = %i; %silly:b = %i; %silly:c = %i;
       %s = $lstr_get_image('SILLY')
       %s = [[$Lstr Get Image and $Lstr Set Image|$lstr_get_image]]('SILLY')
       printText {~} = {%s:stringToHex}
       printText {~} = {%s:[[StringToHex (String_function)|stringToHex]]}
       printText {~} = {%s:substring(1, 4):binaryToFloat}
       printText {~} = {%s:[[Substring (String function)|subString]](1, 4):binaryToFloat}
       printText {~} = {%s:substring(5, 8):binaryToFloat}
       printText {~} = {%s:substring(5, 8):binaryToFloat}
       printText {~} = {%s:substring(13, 16):binaryToFloat}
       printText {~} = {%s:substring(13, 16):binaryToFloat}
   end for
   end for
end
end
</p>
</p>
The result is:
The result is:
<p class="output">-3.3:
<p class="output">-3.3:
%s:stringToHex = C134CCCDC134CCCCCCCCCCCCC134CCCCCCCCCCCC0000000000000000
%s:stringToHex = C134CCCDC134CCCCCCCCCCCCC134CCCCCCCCCCCC0000000000000000
%s:substring(1, 4):binaryTo<var>Float</var> = -3.30000019073486
%s:substring(1, 4):binaryToFloat = -3.30000019073486
%s:substring(5, 8):binaryTo<var>Float</var> = -3.3
%s:substring(5, 8):binaryToFloat = -3.3
%s:substring(13, 16):binaryTo<var>Float</var> = -3.3
%s:substring(13, 16):binaryToFloat = -3.3
-2.2:
-2.2:
%s:stringToHex = C1233333C123333333333333C1233333333333330000000000000000
%s:stringToHex = C1233333C123333333333333C1233333333333330000000000000000
%s:substring(1, 4):binaryTo<var>Float</var> = -2.19999980926514
%s:substring(1, 4):binaryToFloat = -2.19999980926514
%s:substring(5, 8):binaryTo<var>Float</var> = -2.2
%s:substring(5, 8):binaryToFloat = -2.2
%s:substring(13, 16):binaryTo<var>Float</var> = -2.2
%s:substring(13, 16):binaryToFloat = -2.2
-1.1:
-1.1:
%s:stringToHex = C111999AC111999999999999C1119999999999990000000000000000
%s:stringToHex = C111999AC111999999999999C1119999999999990000000000000000
%s:substring(1, 4):binaryTo<var>Float</var> = -1.10000038146973
%s:substring(1, 4):binaryToFloat = -1.10000038146973
%s:substring(5, 8):binaryTo<var>Float</var> = -1.1
%s:substring(5, 8):binaryToFloat = -1.1
%s:substring(13, 16):binaryTo<var>Float</var> = -1.1
%s:substring(13, 16):binaryToFloat = -1.1
   0:
   0:
%s:stringToHex = 00000000000000000000000000000000000000000000000000000000
%s:stringToHex = 00000000000000000000000000000000000000000000000000000000
%s:substring(1, 4):binaryTo<var>Float</var> = 0
%s:substring(1, 4):binaryToFloat = 0
%s:substring(5, 8):binaryTo<var>Float</var> = 0
%s:substring(5, 8):binaryToFloat = 0
%s:substring(13, 16):binaryTo<var>Float</var> = 0
%s:substring(13, 16):binaryToFloat = 0
  1.1:
  1.1:
%s:stringToHex = 4111999A411199999999999941119999999999990000000000000000
%s:stringToHex = 4111999A411199999999999941119999999999990000000000000000
%s:substring(1, 4):binaryTo<var>Float</var> = 1.10000038146973
%s:substring(1, 4):binaryToFloat = 1.10000038146973
%s:substring(5, 8):binaryTo<var>Float</var> = 1.1
%s:substring(5, 8):binaryToFloat = 1.1
%s:substring(13, 16):binaryTo<var>Float</var> = 1.1
%s:substring(13, 16):binaryToFloat = 1.1
  2.2:
  2.2:
%s:stringToHex = 41233333412333333333333341233333333333330000000000000000
%s:stringToHex = 41233333412333333333333341233333333333330000000000000000
%s:substring(1, 4):binaryTo<var>Float</var> = 2.19999980926514
%s:substring(1, 4):binaryToFloat = 2.19999980926514
%s:substring(5, 8):binaryTo<var>Float</var> = 2.2
%s:substring(5, 8):binaryToFloat = 2.2
%s:substring(13, 16):binaryTo<var>Float</var> = 2.2
%s:substring(13, 16):binaryToFloat = 2.2
  3.3:
  3.3:
%s:stringToHex = 4134CCCD4134CCCCCCCCCCCC4134CCCCCCCCCCCC0000000000000000
%s:stringToHex = 4134CCCD4134CCCCCCCCCCCC4134CCCCCCCCCCCC0000000000000000
%s:substring(1, 4):binaryTo<var>Float</var> = 3.30000019073486
%s:substring(1, 4):binaryToFloat = 3.30000019073486
%s:substring(5, 8):binaryTo<var>Float</var> = 3.3
%s:substring(5, 8):binaryToFloat = 3.3
%s:substring(13, 16):binaryTo<var>Float</var> = 3.3
%s:substring(13, 16):binaryToFloat = 3.3
</p>


</p>
==See also==
==See also==
[[List of intrinsic String methods]]
<ul><li>The inverse of the <var>BinaryToFloat</var> method is <var>[[FloatToBinary (Float function)|FloatToBinary]]</var>.</ul>
 
{{Template:String:BinaryToFloat footer}}
 
[[Category:String methods|BinaryToFloat function]]
[[Category:Intrinsic methods]]

Latest revision as of 18:36, 2 November 2012

Convert binary string representation of a number to floating point (String class)


BinaryToFloat is an intrinsic function converts a binary string representation of a floating point number to that number.

Syntax

%number = string:BinaryToFloat

Syntax terms

%number A numeric variable to receive the float value of the method object string.
string A binary string value that can have a length of 4, 8, or 16 bytes.

Usage notes

  • BinaryToFloat is available as of Sirius Mods version 7.5.

Examples

The following program demonstrates the BinaryToFloat method, converting a series of different-length representations of the same float value.

begin image silly a is float len 4 b is float len 8 c is float len 16 end image prepare image silly %i is float %s is string len 30 for %i from -3.3 to 3.3 by 1.1 printText {%i:right(15)}: %silly:a = %i; %silly:b = %i; %silly:c = %i; %s = $lstr_get_image('SILLY') printText {~} = {%s:stringToHex} printText {~} = {%s:subString(1, 4):binaryToFloat} printText {~} = {%s:substring(5, 8):binaryToFloat} printText {~} = {%s:substring(13, 16):binaryToFloat} end for end

The result is:

-3.3: %s:stringToHex = C134CCCDC134CCCCCCCCCCCCC134CCCCCCCCCCCC0000000000000000 %s:substring(1, 4):binaryToFloat = -3.30000019073486 %s:substring(5, 8):binaryToFloat = -3.3 %s:substring(13, 16):binaryToFloat = -3.3 -2.2: %s:stringToHex = C1233333C123333333333333C1233333333333330000000000000000 %s:substring(1, 4):binaryToFloat = -2.19999980926514 %s:substring(5, 8):binaryToFloat = -2.2 %s:substring(13, 16):binaryToFloat = -2.2 -1.1: %s:stringToHex = C111999AC111999999999999C1119999999999990000000000000000 %s:substring(1, 4):binaryToFloat = -1.10000038146973 %s:substring(5, 8):binaryToFloat = -1.1 %s:substring(13, 16):binaryToFloat = -1.1 0: %s:stringToHex = 00000000000000000000000000000000000000000000000000000000 %s:substring(1, 4):binaryToFloat = 0 %s:substring(5, 8):binaryToFloat = 0 %s:substring(13, 16):binaryToFloat = 0 1.1: %s:stringToHex = 4111999A411199999999999941119999999999990000000000000000 %s:substring(1, 4):binaryToFloat = 1.10000038146973 %s:substring(5, 8):binaryToFloat = 1.1 %s:substring(13, 16):binaryToFloat = 1.1 2.2: %s:stringToHex = 41233333412333333333333341233333333333330000000000000000 %s:substring(1, 4):binaryToFloat = 2.19999980926514 %s:substring(5, 8):binaryToFloat = 2.2 %s:substring(13, 16):binaryToFloat = 2.2 3.3: %s:stringToHex = 4134CCCD4134CCCCCCCCCCCC4134CCCCCCCCCCCC0000000000000000 %s:substring(1, 4):binaryToFloat = 3.30000019073486 %s:substring(5, 8):binaryToFloat = 3.3 %s:substring(13, 16):binaryToFloat = 3.3

See also