Martin Atkins (mart) wrote in fotobilder,
Martin Atkins

On Floating Point Arithmetic in S2

At the risk of going a little off-topic, I want to push this thought again. The follwing is an email I wrote to Brad in April, which refers to a previous message I wrote where I fixed a bug which would allow fractional numbers to get into S2's integer variables by doing division operations.

I wrote not long ago about the problem with division sometimes resulting in fractions stored in S2 integers. I said I once thought it was useful, but changed my mind.

However, now I've remembered why I thought it was useful. Allow me to share some code from a ‘funky’ layout I've been working on as a side-project…

if ($e.userpic.height != 0) {
    var int userpicaspect;
    var int userpicheight;
    $userpicaspect = $e.userpic.width / $e.userpic.height;
    $userpicheight = 50 / $userpicaspect;

This allows me to display userpics at half-size, which was working wonderfully before aside from the fact that it was occasionally giving fractional values for the height, which is invalid HTML. It was the invalid HTML which made me get “annoyed” with it, but I temporarily forgot about my aspect variable.

Having just found that my layout now often crashes out with a “division by zero” error (which spurred my recent email) I was confused. However, I see why now.

$userpicaspect can't contain fractional numbers, so my aspect calculations are getting thrown off and distorting the aspect ratio of the userpics I'm printing. Worse, with a width of 98 and a height of 100, the result is 0.98 (obviously) which gets rounded to zero, causing the error.

Is there some way you can allow me to store or at least make use of a float value for this purpose? I'm not really sure how, but it would be a good thing, as I'm sure this isn't the only application for floating point arithmetic in S2…

Brad responded:

i can add nums… beside just ints

To which I responded,

Although you've probably already thought this out, here's what I'd like to see from nums and ints…

Firstly, here's the variables I'll be using in my examples ;)

var num somenumber;
var int intone;
var int inttwo;
var numorint anynumber; # (made that type up for the sake of example ;) )
$somenumber = $intone / $inttwo;

compiles to: $somenumber = $intone / $inttwo;

$intone = $anynumber;

compiles to: $intone = int($anynumber)

That is, any integer assignment gets the second operand wrapped in int() automatically, unless the second operand itself has type int.

This second thing removes the need to special-case integer division, as the integers themselves will automatically remove the decimal part when getting assigned. This also means that:

$somenumber = $intone / $inttwo;
$intone = $somenumber;

…will never result in $intone containing a decimal.

I also wrote some stuff about implicit conversions with function calls which you can hopefully imagine for yourselves, and I noted that S2 doesn't have “floating-point” literals. Brad said:

Idea regarding nums/ints: Just change ‘int’ type to ‘num’ and make users use int(num n) when necessary?

So, I said:

Hmm... I'd prefer if the core layer could restrict function calls to ints for ease-of-use sake, but if that'd make it far to complex then this solution would be alright, I guess.

Brad was right about my answers! ;)

Looking back, I'm not really sure anymore that a floating point/integer distinction would really be user-friendly. Most average users don't even know what an lnteger is, let alone what "floating point" means. Everyone knows what a number is, though.

While it would mean a lot of search and replace both in the already-existing layers and in the compiler code, I think it'd be good to change S2's numeric datatype to be floating-point now rather than later. The int() call (to obtain the integer part of the number) can be optimised directly onto perl's built in function, and we should also provide a function which rounds to the nearest integer rather than just lopping off the fractional component.


  • 302: lj_dev

    In the interests of consolidating all FotoBilder development-related discussion, we're going to be closing down this community. The same…

  • Development stalled?

    Is the development of Fotobilder held? Stalled? I am asking because there is no activity on the community and there is no link to the Fotobilder…

  • (no subject)

    Does FotoBilder works with Apache2? I installed all the required modules on my debian sarge, and when I restart my apache server, it dies horribly…

  • Post a new comment


    Comments allowed for members only

    Anonymous comments are disabled in this journal

    default userpic