Creating a Float from bytes

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Creating a Float from bytes

Bernhard Pieber-3
Hi all!

The following code used to work in Dolphin 4.0 but it does not any more in
Dolphin 5.0:

| result |
result := Float new.
result
 at: 8 put: 16r3F.
 at: 7 put: 16rF0;
 at: 6 put: 0;
 at: 5 put: 0;
 at: 4 put: 0;
 at: 3 put: 0;
 at: 2 put: 0;
 at: 1 put: 0;
result = Float one

It fails because at:put: is not allowed anymore for Floats. What is the
official method of achieving the desired effect in 5.0? And does anyone know
if there is an ANSI-compatible way of doing it?

Thanks!

Bernhard Pieber


Reply | Threaded
Open this post in threaded view
|

Re: Creating a Float from bytes

Ian Bartholomew-17
Bernhard,

> It fails because at:put: is not allowed anymore for Floats. What is the
> official method of achieving the desired effect in 5.0?

I don't think it will be "official" but as Chris (?) pointed out the other
day (in a different context) you could always use the private method
#basicAt:

result := Float new.
result
    basicAt: 8 put: 16r3F;
    basicAt: 7 put: 16rF0;
    basicAt: 6 put: 0;
    basicAt: 5 put: 0;
    basicAt: 4 put: 0;
    basicAt: 3 put: 0;
    basicAt: 2 put: 0;
    basicAt: 1 put: 0.
result = Float one

The comment for the override method in Number>>at:put: implies that it is
aimed at SmallIntegers so I am not sure if it was also intended to apply to
Floats? (but see below).

>            And does anyone know
> if there is an ANSI-compatible way of doing it?

I very much doubt it.  ANSI allows for three different Float precisions but
makes no statement about the underlying representation.

I'm also not sure it is a good idea in Dolphin as there is no guarantee that
OA won't change the Float format (didn't there use to be Single and Double
Float classes?)

Ian


Reply | Threaded
Open this post in threaded view
|

Re: Creating a Float from bytes

Blair McGlashan
"Ian Bartholomew" <[hidden email]> wrote in message
news:og1a9.963$J47.108441@stones...
> Bernhard,
>
> > It fails because at:put: is not allowed anymore for Floats. What is the
> > official method of achieving the desired effect in 5.0?
>
> I don't think it will be "official" but as Chris (?) pointed out the other
> day (in a different context) you could always use the private method
> #basicAt:
> [... snip ...]

Alternatively you could use the FFI primitive responsible for pulling
doubles out of structures, e.g

    #[0 0 0 0 0 0 16rF0 16r3F] doubleAtOffset: 0

This is likely to be several times faster too, although in either case if
you are just creating a constant/literal then you could make use of the ##()
construct.

> ...
> The comment for the override method in Number>>at:put: implies that it is
> aimed at SmallIntegers so I am not sure if it was also intended to apply
to
> Floats? (but see below).

All Numbers should be treated as immutable, whether or not they actually are
is an implementation detail. Although double-precision floats are
implemented as full blown Objects in 32-bit Dolphin, a 64-bit implementation
might use a tagged pointer (as SmallInteger's) to achieve a significant
performance gain.

>
> >            And does anyone know
> > if there is an ANSI-compatible way of doing it?
>
> I very much doubt it.  ANSI allows for three different Float precisions
but
> makes no statement about the underlying representation.
>...

You're probably on fairly safe ground assuming  that an IEEE
double-precision Float will be available, but I don't think you can assume
anything about how that would be represented as an Object.

Regards

Blair


Reply | Threaded
Open this post in threaded view
|

Re: Creating a Float from bytes

Bernhard Pieber-3
"Blair McGlashan" <[hidden email]> schrieb im Newsbeitrag
news:aki58o$1ikrb7$[hidden email]...
> Alternatively you could use the FFI primitive responsible for pulling
> doubles out of structures, e.g
>
>     #[0 0 0 0 0 0 16rF0 16r3F] doubleAtOffset: 0
>
> This is likely to be several times faster too, although in either case if
> you are just creating a constant/literal then you could make use of the
##()
> construct.
Thanks for your answer. I had found this method myself in the meantime but
it is good to know that this is proper way of doing what I want.

> All Numbers should be treated as immutable, whether or not they actually
are
> is an implementation detail.
Well yes. I don't want to change the Float once I have created it. I just
want to instantiate one which I get sent over a Socket. I was not quite sure
whether the encoding would be exactly the same. So far it works. But what
would happen if I would get a denormalized Float? I ask because Float
denormalized answers false in Dolphin. But wait a minute, I'll give it a
try.

#[2r00000000 2r00000000 2r00000000 2r00000000 2r00000000 2r00000000
2r00000000 2r00000001] reverse doubleAtOffset: 0

The answer ist 4.94065645841247e-324. So it seems Dolphin does support
denormalized Floats after all. But then Float denormalized should answer
true, shouldn't it?

> > >            And does anyone know
> > > if there is an ANSI-compatible way of doing it?
> >
> > I very much doubt it.  ANSI allows for three different Float precisions
> but
> > makes no statement about the underlying representation.
> >...
>
> You're probably on fairly safe ground assuming  that an IEEE
> double-precision Float will be available, but I don't think you can assume
> anything about how that would be represented as an Object.
Still I think it would be nice if I could instantiate a Float from a
Socket/ByteArray in a portable way.

Anyway, thank you again for your answer!

Bernhard


Reply | Threaded
Open this post in threaded view
|

Re: Creating a Float from bytes

rush
In reply to this post by Blair McGlashan
"Blair McGlashan" <[hidden email]> wrote in message
news:aki58o$1ikrb7$[hidden email]...
> You're probably on fairly safe ground assuming  that an IEEE
> double-precision Float will be available, but I don't think you can assume
> anything about how that would be represented as an Object.

I do not know for others but for me it is important that I can reliably
convert from and into IEEE byte format, and as long as dword:atOffset does
that I am fine.

rush