Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For this purpose the best possible solution is to convert into bytes using IEEE754-1985 standard using SQL commands.</p> <p>First it is required to check for special cases defined by IEEE754-1985 standard. Then just follow the standard algorithm to convert if it is not in any special cases. Sample code is below.</p> <p>Inputs are <code>bytea_value bytea, is_little_endian boolean</code> then divide into 4 bytes as below: </p> <pre><code> byte_array[0]:= get_byte(bytea_value, 0); byte_array[1]:= get_byte(bytea_value, 1); byte_array[2]:= get_byte(bytea_value, 2); byte_array[3]:= get_byte(bytea_value, 3); </code></pre> <p>Then get the binary value by considering little endian or big endian</p> <pre><code>IF is_little_endian THEN binary_value:= byte_array[0]::bit(8) || byte_array[1]::bit(8) || byte_array[2]::bit(8) || byte_array[3]::bit(8); ELSE binary_value:= byte_array[3]::bit(8) || byte_array[2]::bit(8) || byte_array[1]::bit(8) || byte_array[0]::bit(8); END IF; </code></pre> <p>Now check for special cases:</p> <pre><code>IF binary_value = '00000000000000000000000000000000' OR binary_value = '10000000000000000000000000000000' THEN -- IEEE754-1985 Zero return 0.0; END IF; sign := substring(binary_value from 1 for 1); exponent := substring(binary_value from 2 for 8); mantissa := substring(binary_value from 10 for 23); IF exponent = '11111111' THEN IF mantissa = '00000000000000000000000' THEN -- IEEE754-1985 negative and positive infinity IF sign = '1' THEN return '-Infinity'; ELSE return 'Infinity'; END IF; ELSE return 'NaN'; -- IEEE754-1985 Not a number END IF; END IF; </code></pre> <p>If it does not belong to any special cases just convert it as below:</p> <pre><code>exp := exponent::int; IF exp &gt; 126 THEN exp := exp - 127; ELSE exp:= -exp; END IF; WHILE mantissa_index &lt; 24 LOOP IF substring(mantissa from mantissa_index for 1) = '1' THEN result := result + power(2, -(mantissa_index)); END IF; mantissa_index = mantissa_index + 1; END LOOP; result := result * power(2, exp); IF(sign = '1') THEN result = -result; END IF; return result; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload