Casting an Integer to a Single, preserving bit representation
Date : March 29 2020, 07:55 AM
wish helps you Is there a fast way in VB.NET to take a 32-bit int and cast to a 32-bit float while preserving the underlying bit structure? BitConverter will do this, but I'd like to cast it directly without involving byte arrays. , Damn, how could I possibly forget about The C-style Union? <Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Explicit)> _
Public Structure IntFloatUnion
<Runtime.InteropServices.FieldOffset(0)> Public i As Integer
<Runtime.InteropServices.FieldOffset(0)> Public f As Single
End Structure
Sub Main()
Dim u As IntFloatUnion
u.i = 42
Console.WriteLine(u.f)
Console.ReadLine()
End Sub
public static class FancyConverter
{
public static unsafe float FloatFromBytes(int i)
{
return *((float*)(void*)(&i));
}
}
|
Floating point representation can support larger values in comparision to integer representation
Tag : java , By : Vorinowsky
Date : March 29 2020, 07:55 AM
I wish this helpful for you Suppose you assume this convention: the last two digits of your integer denote the power-of-10 of the remainder. Then you can store numbers up to 21474835 * 10^99 in a single integer. (You would also need to program all arithmetic methods to use it a such.) To use floating point numbers in the same way, you can use the same convention. Since a signed integer stores 9 decimals (10, but the first one can only be 0, 1 or 2), you could say that the decimal period is behind the first decimal. So to store "1234.56" you would use 123456004
|
casting a integer variable to float
Date : March 29 2020, 07:55 AM
hope this fix your issue After spending a few hours reading up on synthesizing loops and trying to translate the to_float with integer arg I had a thought: library ieee;
library ieee_proposed;
use ieee_proposed.float_pkg.all;
use ieee.numeric_std.all;
entity SM is
end entity;
architecture foo of SM is
-- From float_pkg_c.vhdl line 391/3927 (package float_pkg):
-- -- to_float (signed)
-- function to_float (
-- arg : SIGNED;
-- constant exponent_width : NATURAL := float_exponent_width; -- length of FP output exponent
-- constant fraction_width : NATURAL := float_fraction_width; -- length of FP output fraction
-- constant round_style : round_type := float_round_style) -- rounding option
-- return UNRESOLVED_float is
begin
UNLABELLED:
process
variable denum : integer;
variable num : integer;
variable dividend : float (4 downto -27);
begin
denum := 42;
num := 21;
dividend := to_float(TO_SIGNED(num,32), 4, 27) / to_float(TO_SIGNED(denum,32), 4, 27);
assert dividend /= 0.5
report "dividend = " & to_string(dividend)
severity NOTE;
wait;
end process;
end architecture;
|
Turn integer representation of a price to a double representation
Tag : mysql , By : Gianluca Riccardi
Date : March 29 2020, 07:55 AM
should help you out I have a table called products. It stores the name of products and their price. The price is stored as cents in the format of an integer. , How about SELECT name, price/100 as price FROM products
|
get the integer representation value of a float type variable in C
Tag : c , By : FarmerDave
Date : March 29 2020, 07:55 AM
With these it helps I have the number 20 (0x14) stored in a 32-bit register. The register is allocated to a C float variable representing the value 2.8e-44. Now I want to get the hexadecimal representation of the float variable back and stored it into an integer variable in order to restore the original meaning of the information. Is there a better way to do that, apart from doing some pointer business? Here is the code: , You can achieve your need with a union: #include <stdio.h>
int main()
{
union { int i; float f; } fi;
fi.f = 2.802597e-44;
printf("f=%e\n",fi.f);
printf("n=%d\n",fi.i);
return 0;
}
|