Declaring a Swift type

As an exercise, I’d like to implement Swift’s Float80 in Xojo but I can’t figure out the right declares to instantiate. Any help?

Interesting… When I first read your post my initial reaction was “There is no such thing”
but I was wrong… gonna have to see what potential benefit that has…

Looks like it removes the current “issues” with “DOUBLE” if used properly… bet its an 80 bit value as opposed to 64

Right, which is an odd thing because it still uses 16 bytes, if I read correctly, and a quadruple-precision type (yes, that’s a thing too) uses all 128 bits.

But I’d still like to know if/how to declare it to for use within Xojo.

I would say you can’t… you might make a class that “emulates” it…

What I think would be really cool… was if Xojo could directly include SWIFT code into an macOS/iOS project… I’d have SO many custom controls to offer… :slight_smile:

This may help:

A memory block that is 10 bytes may work.

This definition for a Float80 in Xcode headers might help (It’s in MacTypes.h in the 10.13 SDK):

struct Float80 {
    SInt16  exp;
    UInt16  man[4];
typedef struct Float80 Float80;

DOUBLE uses 8 bytes

a FLOAT80 uses 10

You still need some logic to encode/decode to the proper IEEE standard…
and even then Xojo has no “container” to represent it as a numeric value

@Jason Parsley, I appreciate the reference. I have been doing a lot of reading about this of late as I was curious how Doubles were stored, and that lead me to quadruple-precision and, finally, Float80. Emulating one of these is no easy feat as you have to implement the various math operations manually too, and find a way to convert it to human-readable form. You’d be better off just using @Robert Delaney 's fpPlugin.

But as I mentioned at the start, this has turned into an exercise, not something I really need, so I’m still curious if there is a way to declare it for use like you might, for example, NSNumber.

For those that share my curiosity, the Double is stored like this:

Bit 1 = sign
Bits 2 - 12 = exponent. Read it as an UInt, subtract 1023, and raise 2 to this power (all zeros and all ones have special meanings).
Bits 13 - 64 = The remaining value. If the exponent above is n, then bit 13 represents 2^(n-1), bit 14 is 2^(n-2), and down the line until bit 64.

Interesting point: because of this storage scheme, the last whole number a Double can hold in sequence is 2^53. After that, it starts approximating.

I am sure that there is, but most of the public knowledge is based upon the work by Charles Yeomans (whom I haven’t see for a while). I would imagine that @Joe Ranieri from Xojo might have an idea about it.

Obj-C to me is an impressive language/toolbox. Once you’ve overcome the hurdle of learning Obj-C quirks, it’s a very verbose1 language with tremendous power behind it. Last time I sat down and tried to experiment with Swift, it was far less capable than Xojo. I’m sure that it’s changed now, but looking through the docs I don’t see any where near as much power2 as Xojo or Objective-C, but that could be because I’m looking in the wrong place.

*1 I have come to appreciate methods with tell what you properties they take in their name, especially constructors. It’s easier to type new object( inprop ) than object withPropType( inProp ) but it makes it a lot easier when you’re over-viewing older code or even working new objects.
*2 An example would be the ability to modify an object during runtime, not just properties but to redirect functions and events by changing the function/event addresses.

I would just use the fp plugin out there or my own BigNumberMBS class…

It would be nice if Xojo itself would have more fully supported data types.

Thank you, but again, the point isn’t the type itself, it’s how to declare it in Xojo, or if it’s even possible. It could be any of the swift types.

Well, you can figure out the declarations for the library functions doing the basic operations like add, mul, div and sub as well as comparison.
Than use a memory block with 80 bits (10 bytes) for storage and call the methods as needed.

That of course presumes that the values are passed by pointer and not by register!

The point is, you can’t “declare a Swift datatype”… you can only create a class the emulates it

That’s right.
But I don’t think you can overload all operators in Xojo, right?

I think you can. Which ones are missing?

Nil as 0 won’t work for example.

And you can’t have an operator taking double and one taking int64 without conflicts.

So doing some Goggling and it seems that Swift guys resort back to Objective to be able to dynamically create class instances, which is fine except that it only works for the swift classes that are based on NSObject.

Looking at Apple’s docs this is a base class, by taking what I’ve discovered from reading; even Swift guys can’t create this dynamically.

So in summary, I don’t think we can do it with the knowledge that we have now. If you were to create a Xojo plugin using Swift you could hard code the object and once you’ve gotten the instanceID you can access it via declares in Xojo.

NSOBJECT IS the base object for just about everything in UIKit

here is the hierachry of most of UIKit and how it stems from NSObject

I’ll have to ask @Joe Ranieri about this at XDC. I just hope I can understand his answer. :slight_smile: