I'm trying to convert the binary output from an IMU sensor (VectorNav VN100) to a float. The example given in the user manual is shown here.
In the example in the user manual (pg 41) the hexadecimal digit 0x422E5093 converts to a float value of +43.578686.... how???
When I convert I get a float value of 1110331539
Problem solved, it was a misunderstanding of the problem on my part.
An online floating point to hex converter outlines the procedure for converting. Hopefully this will be of use to someone else!
Related
I'm a total Haskell beginner who just discovered that read spits out an exception when given a decimal number starting with . rather than a digit. For example, in ghci:
Prelude> read ".7" :: Float
*** Exception: Prelude.read: no parse
I found one discussion and it makes sense why surrounding . in numbers with digits is required in Haskell. Another discussion is also somewhat helpful, but no one provides a solution of how to actually convert ".7" to 0.7.
So, I'm trying to extract data from a fixed-width format file containing fields with values like .7---is there a standard function or approach I can use to clean this up to a float 0.7?
(Before I hit this issue, my basic ideas was to define a custom type for my data, use splitWidth in Data.List.Split to split each line into its fields, and then use read to convert each field into its correct type, trying to apply the functional goodness in this answer in the actual implementation.)
As Thomas M. DuBuisson answered in a comment above, the obvious thing to do is myRead = read . ('0':) :: String -> Float. This works for me --- I won't ever be trying to read negative numbers, and I know which fields should be read as float. Thanks!
My Metal compute kernel writes to a texture with format MTLPixelFormat.RG16Float, half precision floating point numbers. This is because Metal does not support writing to 32 bits floating point textures.
I need to read these half precision numbers in my swift program? I have moved the texture into a Swift UInt8 array, but I cannot figure out how to convert the half precision floats into Swift floats.
Actually #Muzza's answer is not correct. You could have read them from a float16_t pointer and cast them to a normal float32_t. No need to use external libraries. Just import the arm_neon header.
EDIT: the below answer was written for Swift v1. Later versions of Swift added support via the float16_t type. Manual conversion may still be useful in some cases.
There is no built in method to interpret halfs as floats, you have to convert it yourself.
The half datatype is a floating point format following IEEE 754. You can read the values in your swift program using UInt16 and then convert from this to a float value.
I don't know of any conversion routines written in Swift, but here are two libraries written in C++ that could be converted easily enough:
http://mrob.com/pub/math/s10e5.h.txt
http://half.sourceforge.net/
In the first library it is the function operator float() that you need to convert, that takes the UInt16 member variable _h and outputs a float.
R15b on Windows gives:
>trunc(1.9999999999999999999).
2
For that matter, just typing the float returns:
> 1.9999999999999999999.
2.0
AFAIK, the truncate function should just drop the fractional portion (at least that's what I need, anyway). A floor function might also do the trick AFAIK, but the floor implementations I've seen posted online use... you guessed it... trunc.
I'm not nitpicking this, I actually need this to be correct for a program I'm developing.
Any ideas on this?
Thanks.
Your problem is decimal numbers are represented as IEEE compliant binary representation (32, 64 or 128 bit).
If you really need precision you should use other numerical data structures as Binary Coded Decimal or fixed-point arithmetic.
Hope this helps!
if you want to make a TRUNC to float, maybe this one can help:
select substring (convert(varchar(14), CAST (20160303013458 as varchar(14))) , 1 , 8)
I need to parse floating-point literals in C code using OCaml.
OCaml's float type is 64 bit. I have the string of the literal, its numeric value rounded to 64 bits and its kind (float, double or long double).
The problem are literals with a numeric value bigger than 64 bit:
long double literals
float literals with 'f'-suffix for which double rounding errors would occur if they wouldn't have the suffix.
OCaml's arbitrary-precision module can parse rational numbers from strings like "123/123", but not "123.123", "123e123", "0x1.23p-1" like they might appear in C.
Background: I do value analysis of C programs using CIL.
Double literals of any size and float literals with a numeric value that fits into 64 bit are always correctly represented. By rounding from double- to single-precision I can also reproduce double rounding errors.
I wrote my answer in the form of a blog post
To summarize some of the points here: you could interface strtold() and strtof() from OCaml. For the former, you would have to consider how you are going to store the result it produces, since there only is a point if long double is larger than double on your host architecture. There remains the problem that these functions are buggy in one of the most widely used C library. Very slightly buggy, but buggy for exactly the examples that are going to be of interest if you are doing this to study double rounding.
Another way is to write your own function, starting from another post in the blog you refer to.
Finally, the phrase "Even getting single-precision floats right requires me to parse literals with values bigger than 64 bit" that you use in the comments is still a strange way to put it. The intermediate format(s) in which you can parse the representation of a single-precision float before you round it to single-precision have to be lossless, otherwise there will be double rounding. Double rounding may be more or less difficult to exhibit depending on the precision of the lossy intermediate format, but using 80 bits or 128 bits binary floating-point formats is not going to remove the problem, just make it more subtle. In the simple algorithm that I recommend, the intermediate format is a fraction of two multiprecision integers.
I don't see the question in this question :)
Assuming that you need an ocaml parser for "C float literals" - the answer is - write one yourself, it is not very hard and you will have strict control on the implementation details and what "C float literal" actually means.
Given a double value like 1.00500000274996E-8, how do I convert it to it's non scientific format with a maximum number of digits after the decimal point - in this case with 8 digits it would be 1.00500000?
The conversion should not pad with zeros, so 2007 would come out as 2007, and 2012.33 and 2012.33.
I've tried lots of combinations using Format, FormatFloat, FloatToStrF but can't quite seem to hit the jackpot. Many thanks for any help.
Edit: I should clarify that I am trying to convert it to a string representation, without the Exponent (E) part.
FormatFloat('0.######################', 1.00500000274996E-8) should do the trick.
Output is: 0,0000000100500000274996
It will not output more digits than absolutely necessary.
See John Herbster's Exact Float to String Routines in CodeCentral. Perhaps not exactly what youre after but might be good starting point... CC item's description:
This module includes
(a) functions for converting a floating binary point number to its *exact* decimal representation in an AnsiString;
(b) functions for parsing the floating point types into sign, exponent, and mantissa; and
(c) function for analyzing a extended float number into its type (zero, normal, infinity, etc.)
Its intended use is for trouble shooting problems with floating point numbers.
His DecimalRounding routines might be of intrest too.