Long Double in Swift - swift

In C, there is a type called long double, which on my machine is 16-bytes (128-bit). Is there any way to store a long double in Swift? I've tried type aliases (typedefs) of long double from an Objective-C header, but it doesn't show up in Swift. And functions returning long double (powl, sqrtl) don't show up neither.
I know there's a Float80 but that's only 80 bits, not 128 bits.

Unless you have a POWER processor, your 128 bit long double is actually an 80 bits extended precision number, plus 48 bit wasted.

Related

Swift Difference Between Double and Float64

In Swift Programing language official documentation, It says
Double represents a 64-bit floating-point number.
Float represents a 32-bit floating-point number.
Link: https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID321
Then, Why is there Float64? What is the difference between them? Or Are they same?
The headers, found by hitting command+shift+o and searching for Float64, say:
/// A 64-bit floating point type.
public typealias Float64 = Double
/// A 32-bit floating point type.
public typealias Float32 = Float
and
Base floating point types
Float32 32 bit IEEE float: 1 sign bit, 8 exponent bits, 23 fraction bits
Float64 64 bit IEEE float: 1 sign bit, 11 exponent bits, 52 fraction bits
Float80 80 bit MacOS float: 1 sign bit, 15 exponent bits, 1 integer bit, 63 fraction bits
Float96 96 bit 68881 float: 1 sign bit, 15 exponent bits, 16 pad bits, 1 integer bit, 63 fraction bits
Note: These are fixed size floating point types, useful when writing a floating
point value to disk. If your compiler does not support a particular size
float, a struct is used instead.
Use of of the NCEG types (e.g. double_t) or an ANSI C type (e.g. double) if
you want a floating point representation that is natural for any given
compiler, but might be a different size on different compilers.
As a general rule, unless you’re writing code that is dependent on binary representations, you should use the standard Float v Double names. But if you are writing something where binary compatibility is needed (e.g. writing/parsing binary Data to be exchanged with some other platform), then you can use the data types that bear the number of bits in the name, e.g. Float32 vs. Float64 vs. Float80.
Go to the definiation of Float64
/// A 64-bit floating point type.
public typealias Float64 = Double
/// A 32-bit floating point type.
public typealias Float32 = Float

LReal Vs Real Data Types

In PLC Structure Text what is main difference between a LReal Vs a Real data type? Which would you use when replacing a double or float when converting from a C based language to structure text with a PLC
LReal is a double precision real, float, or floating point variables that is a 64 bit signed value rather then a real is a single precision real, float, or floating point that is made from a 32 bit signed value. So it stores more in a LReal which makes LReal closer to a Double and a Float. The other thing to keep in mind is depending on the PLC it will convert a Real into a LReal for calculations. Plus a LReal is limited to 15 decimal places rather than a Real is 9 decimal places. So if you need more then 9 decimal places I would recommend LReal but if you need less I would stick with Real because with LReals you have to convert from a Integer to a Real to a LReal. So it would save you a step.

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

Is there unsigned Double in Swift?

Forgive me if this is a silly question, I a self-taught programmer. If there is unsigned Int for large whole number storage, there should be an unsigned Double for storing large floating point number like Double right?
According to the Swift Standard Library Reference, if a Double (aka Float64) does not give you enough precision, you can use a Float80. But I have to wonder what it is that are you trying to store that exceeds the capabilities of a Double.
It's because floating point formats, like Double is one, don't support unsigned numbers. Also just because you've got the possibility to use unsigned on some types like Int doesn't mean, that it has to work with others too.

Signed and unsigned integers -- why are bytes treated differently?

I am learning High Level Assembly Language at the moment, and was going over the concept of signed and unsigned integers. It seems simple enough, however getting to sign extension has confused me.
Take the byte 10011010 which I would take to be 154 in decimal. Indeed, using a binary calculator with anything more than word selected shows this as 154 in decimal.
However, if I select the unit to be a byte, and type in 10011010 then suddenly it is treated as -102 in decimal. Whenever I increase it starting from a byte then it is sign extended and will always be -102 in decimal.
If I use anything higher than a byte then it remains 154 in decimal.
Could somebody please explain this seeming disparity?
When you select the unit as a byte the MSB of 10011010 is treated as the signed bit, which makes this one byte signed integer equivalent interpretation to -102 (2's complement).
For integers sized large than 8 bits, say 16 bits the number will be: 0000000010011010 which do not have 1 in MSB therefore it is treated as a positive integer whose integer representation is 154 in decimal. When you convert the 8 bit byte to a higher type the sign extension will preserve the -ve interpretation in the larger length storage too.