Floats in Sophia


#1

Hey I have been reading a bit through the sophia documentation and did a bit of the tutorial, and have these questions:

  • How do you represent floats in Sophia if it only has ints and uints?

  • How would you use an oracle for example, to get that the temperature in Sophia Bulgaria is 15.2 °C if you can’t use float as a data type? Or specify that your erc-20 token has 15 decimal places?

  • How come floats are used as a data type in the sophia tutorial? For example:

    /* this is a tuple of 3 items, explained next */
    type intCoordinates = (int, int, int);
    type floatCoordinates = (float, float, float);

    type coordinates('a) = ('a, 'a, 'a)
    let buddy: coordinates(float) = (10.5, 20.5, 20.5)

or

type coord3d = (float, float, float)
let my3dCoordinates: coord3d = (20.0, 30.5, 100.0)

When I thought floats weren’t used?

Thanks for the answers


#2

Hi @master

Thanks for bringing this up.

With int-s :wink: We currently do not yet have uint-s in Sophia, so please stick to int-s.

If you think about it, floats can easily be represented by int-s from a different denomination. Let’s say you have the number π. Since it is an irrational number, you can not represent it properly with floats. What do we do then? We pick a rounded version that fits our needs. If π = 3.14159..., we might as well use the rounded value of 3.14. Now we have a proper denomination (2 digits after the decimal point) and we can build our math upon this. With ints π would be 314, the number 42 would be represented as 4200 and etc.

It depends on what you need. If a precision of 1 digit after the decimal point is all you need, you can represent 15.2 °C as 152. If you need more precision, you can use a smaller denomination.

Please point me to that tutorial, I could not find it in Sophia’s description. In the whole protocol repository, these are all the mentions of floats:

$ grep -r "float" .
./consensus/consensus.md:a floating point value.
./consensus/consensus.md:Now, the problem is that we can't do any floating point arithmetic (to
./contracts/sophia.md:languages, such as floating point arithmetic, are not present in Sophia, and

Don’t use floats. Floats are evil :slight_smile:


#3

I think that @master follows this tutorial: https://dev.aepps.com/aepp-sdk-docs/Sophia.html
However, there is PR in review that would probably fix that inconsistency: https://github.com/aeternity/aepp-sdk-docs/pull/30/files#r221187875