Convert float to bits in elisp - emacs

How do I get the IEEE 754 binary representation (single or double precision) of an float in elisp.
Only thing I found sofar is this here: http://lists.gnu.org/archive/html/help-gnu-emacs/2002-10/msg00724.html
But I think there must be a better way?

Related

Define single precision variable in Racket

I have to run some computations in Racket that I have never used before.
How do I force it to calculate sth in single or half (if it has those) precision floats?
I figured out how to make it compute in big floats:
(bf/ (bf 1) (bf 7))
I know that the abbreviation for floats (double precision) is fl. I cannot figure out the right abbreviation for single floats though.
The 'bigfloat' package you refer to are for arbitrary precision floating point numbers. You're very unlikely to want these, as you point out.
It sounds like you're looking for standard IEEE 64-bit floating point numbers. Racket uses these by default, for all inexact arithmetic.
So, for instance:
(/ 1 pi)
produces
0.3183098861837907
One possible tripper-upper is that when dividing two rational numbers, the result will again be a rational number. So, for instance,
(/ 12347728 298340194)
produces
6173864/149170097
You can force inexact arithmetic either by using exact->inexact (always works), or by ensuring that your literals end with decimals (unless you're using the htdp languages).
So, for instance:
(/ 12347728.0 298340194.0)
produces
0.04138808061511148
Let me know if this doesn't answer your question....

Swift: Numeric Literals Decimal Floats Exponents [duplicate]

According to The Swift Programming Language :
For example, 0xFp2 represents 15 ⨉ 2^2, which evaluates to 60.
Similarly, 0xFp-2 represents 15 ⨉ 2^(-2), which evaluates to 3.75.
Why is 2 used as base for the exponent instead of 16? I'd have expected 0xFp2 == 15 * (16**2) instead of 0xFp2 == 15 * (2**2)
Swift's hexadecimal notation for floating-point numbers is just a variation of the notation introduced for C in the C99 standard for both input and output (with the printf %a format).
The purpose of that notation is to be both easy to interpret by humans and to let the bits of the IEEE 754 representation be somewhat recognizable. The IEEE 754 representation uses base two. Consequently, for a normal floating-point number, when the number before p is between 1 and 2, the number after p is directly the value of the exponent field of the IEEE 754 representation. This is in line with the dual objectives of human-readability and closeness to the bit representation:
$ cat t.c
#include <stdio.h>
int main(){
printf("%a\n", 3.14);
}
$ gcc t.c && ./a.out
0x1.91eb851eb851fp+1
The number 0x1.91eb851eb851fp+1 can be seen to be slightly above 3 because the exponent is 1 and the significand is near 0x1.9, slightly above 0x1.8, which indicates the exact middle between two powers of two.
This format helps remember that numbers that have a compact representation in decimal are not necessarily simple in binary. In the example above, 3.14 uses all the digits of the significand to approximate (and even so, it isn't represented exactly).
Hexadecimal is used for the number before the p, which corresponds to the significand in the IEEE 754 format, because it is more compact than binary. The significand of an IEEE 754 binary64 number requires 13 hexadecimal digits after 0x1. to represent fully, which is a lot, but in binary 52 digits would be required, which is frankly impractical.
The choice of hexadecimal actually has its drawbacks: because of this choice, the several equivalent representations for the same number are not always easy to recognize as equivalent. For instance, 0x1.3p1 and 0x2.6p0 represent the same number, although their digits have nothing in common. In binary, the two notations would correspond to 0b1.0011p1 and 0b10.011p0, which would be easier to see as equivalent. To take another example, 3.14 can also be represented as 0xc.8f5c28f5c28f8p-2, which is extremely difficult to see as the same number as 0x1.91eb851eb851fp+1. This problem would not exist if the number after the p represented a power of 16, as you suggest in your question, but unicity of the representation was not an objective when C99 was standardized: closeness to the IEEE 754 representation was.

High precision floating point numbers in Swift

How do I handle floating point issue in Swift if Double's precission is not enough? Is there an analog of Java's BigDecimal?
Swift's Float80 is higher-precision (but not arbitrarily large).
http://swiftdoc.org/v2.0/type/Float80/

More precision than double in swift

Are there are any floating points more accurate than Double available in Swift? I know that in C there is the long double, but I can't seem to find its equivalent in Apple's new programming language.
Any help would be greatly appreciated!
Yes there is! There is Float80 exactly for that, it stores 80 bits (duh), 10 bytes. You can use it like any other floating point type. Note that there are Float32, Float64 and Float80 in Swift, where Float32 is just a typealias for Float and Float64 is one for Double
Currently iOS 11+ runs on 64 Bit platform, Double holds Highest among all.
Double has a precision of at least 15 decimal digits, whereas the
precision of Float can be as little as 6 decimal digits. The
appropriate floating-point type to use depends on the nature and range
of values you need to work with in your code. In situations where
either type would be appropriate, Double is preferred.
However in CGFloat The native type used to store the CGFloat, which is Float on 32-bit architectures and Double on 64-bit architectures
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html

Maximum allowed digits after decimal for double

Hi,
I want to know how many number of digits are allowed after decimal
point for primitive double datatype in java, without actually getting
rounded off.
It depends on the number; read up on floating point representations for more information.
Use the BigDecimal class for fixed-scale arithmetic.
Taken literally, the number is 0. Most decimal fractions with at least one digit after the decimal point get rounded on conversion to double. For example, the decimal fraction 0.1 is rounded to 0.1000000000000000055511151231257827021181583404541015625 on conversion to double.
The problem is that double is a binary floating point system, and most decimal fractions can no more be exactly represented in than 1/3 can be exactly represented as a decimal fraction with a finite number of significant digits.
As already recommended, if truly exact representation of decimal fractions is important, use BigDecimal.
The primitive double has, for normal numbers, 53 significant bits, about 15.9 decimal digits. If very, very close is good enough, you can use double.