Integer Maths vs floating point - Script Writing - CHDK Forum  

Integer Maths vs floating point

  • 39 Replies
  • 3018 Views
Integer Maths vs floating point
« on: 09 / April / 2021, 13:14:36 »
Advertisements
I hope one of the CHDK gurus can educate me on the above.


Over in Magic Lantern Lua I use floating point maths.


In CHDK Lua I’m ‘restricted’ to integer maths.


Taking the M3 vs the M, the M3, because it’s CHDK, restricted to integer maths, the M, running with ML, using floating point Lua.


So my question is: is there any hope we might see a floating point Lua implementation on CHDK?


The usual caveat, I’m ignorant about the backend details.


Cheers


Garry

*

Offline reyalp

  • ******
  • 13440
Re: Integer Maths vs floating point
« Reply #1 on: 09 / April / 2021, 14:28:48 »
In CHDK Lua I’m ‘restricted’ to integer maths.
FWIW, you can do fixed point math using the imath library: https://chdk.fandom.com/wiki/Lua/Lua_Reference#Mathematical_functions_.28imath_library.29

Quote
So my question is: is there any hope we might see a floating point Lua implementation on CHDK?
The original decision was because the camera itself does not have an FPU. For most scripting this seems to work well, the camera's native exposure system uses APEX*96 so that exposure calculations can be done with integer math as well. The camera firmware itself does very little floating point.

That said, software FP is available, and performance isn't so hideously slow as to render it unusable. So in principle, offering floating point Lua engine wouldn't be out of the question. See https://chdk.setepontos.com/index.php?topic=12451.msg123421#msg123421

One idea that's been at the back of my mind for a long time is offer multiple Lua backends, so you could have a floating point and fixed implementation, and use a script header parameter like @lua_version 51-float to select. However, the existing CHDK interface assumes numbers are 32 bit ints, so quite a bit of work would be needed to support this.

Lua 5.3 and later support integer and FP natively, which potentially be attractive, but again updating would require significant work, and it's possible there would be significant memory or performance impacts.

So, is there a chance? Yes. Should you hold your breath? Probably not advisable ;)
Don't forget what the H stands for.

Re: Integer Maths vs floating point
« Reply #2 on: 09 / April / 2021, 14:40:42 »
@reyalp


Thanks for the detailed insight.


I do use the imath library, but it still restricts my coding, eg sqrts and multiplying two ‘large’ numbers.


Although I have no chance of implementing it, I hope that a floating point version of CHDK Lua appears...one day. 


Here’s to miracles happening ;-)


Cheers


Garry

Re: Integer Maths vs floating point
« Reply #3 on: 10 / April / 2021, 04:03:07 »
@reyalp


One after thought is related to the current integer math limitation of multiplying two numbers, ie not using the imath library, and ensuring these are less than 2^31, assuming I’ve got that right.

Could there be any value in an integer Lua version able to use larger numbers? That is greater than 32bits.
« Last Edit: 10 / April / 2021, 04:09:35 by pigeonhill »


*

Offline c_joerg

  • *****
  • 1157
Re: Integer Maths vs floating point
« Reply #4 on: 10 / April / 2021, 07:42:55 »
I still remember the time when i did digital signal processing with 16 bit. That was also possible. I don't want to go back either.

Before multiplication, the values ​​were then shifted down until there was no longer any overflow.
M100 100a, M3 101a, G9x II (1.00c), 2*G1x (101a,100e), S110 (103a), SX50 (100c), SX230 (101a), S45,
Flickr https://www.flickr.com/photos/136329431@N06/albums
YouTube https://www.youtube.com/channel/UCrTH0tHy9OYTVDzWIvXEMlw/videos?shelf_id=0&view=0&sort=dd

Re: Integer Maths vs floating point
« Reply #5 on: 10 / April / 2021, 08:25:17 »
@c_joerg


Yes I was thinking along those lines myself, ie test to see if a*b > max, and adjust accordingly, albeit at a little loss.


I’m sure others have looked at this before and come up with an solution, ie using 32 bit calculations on ‘large’ numbers.
« Last Edit: 10 / April / 2021, 08:29:55 by pigeonhill »

*

Offline reyalp

  • ******
  • 13440
Re: Integer Maths vs floating point
« Reply #6 on: 10 / April / 2021, 15:06:24 »
@reyalp
One after thought is related to the current integer math limitation of multiplying two numbers, ie not using the imath library, and ensuring these are less than , assuming I’ve got that right.

Could there be any value in an integer Lua version able to use larger numbers? That is greater than 32bits.
Lua in CHDK is currently configured to use 32 bit (signed) integers for the "number" type. This is what the processor does natively, and Lua as a language does not have a concept of unsigned numbers.

Lua could configured to use 64 bit numbers, but this would have similar performance and memory impacts to using floating point, and would likely have some compatibility issues with  the existing CHDK interface. If someone were going to invest time in this, I'd think the floating point option would be a better choice.

There is another option: You could use metamethods to implement a new datatype that supports the usual mathematical operations for float, 64 bit ints, or even arbitrary precision. While you could theoretically do this in pure Lua, it would be better done with userdata and with C code to implement the underlying operations. This would have the minimum impact on existing code, at the expense of being somewhat less convenient to work with.

A C implementation would still bloat the Lua module, until we add the capability to load C modules in Lua. This is something I want to add, probably higher on my priority list than supporting alternative number types.
Don't forget what the H stands for.

*

Offline Caefix

  • *****
  • 641
  • Sorry, busy deleting test shots...
Re: Integer Maths vs floating point
« Reply #7 on: 10 / April / 2021, 15:36:40 »
 ::) I can´t remember, where I found that calculator script (probably CHDK_DE), might help...
https://forum.chdk-treff.de/viewtopic.php?p=7968#p7968
« Last Edit: 10 / April / 2021, 15:56:20 by Caefix »
All lifetime is a loan from eternity.


Re: Integer Maths vs floating point
« Reply #8 on: 10 / April / 2021, 15:39:39 »
Many thanks to both of you.


I’ll look into the options.


As I said in a recent post on my blog, I got round the current main issue, ie sqrts, by using a different approach.


Cheers


Garry

*

Offline philmoz

  • *****
  • 3332
    • Photos
Re: Integer Maths vs floating point
« Reply #9 on: 26 / April / 2021, 00:18:22 »
One idea - what about adding a single precision floating point equivalent to the 'imathlib' Lua functions?


The data values would still be 32 bit so can be passed around in 'int' variables like imathlib does.
Adding conversion functions to/from imath would also let you mix and match.


Not as convenient as native float variables; but might solve a few problems.


Phil.

CHDK ports:
  sx30is (1.00c, 1.00h, 1.00l, 1.00n & 1.00p)
  g12 (1.00c, 1.00e, 1.00f & 1.00g)
  sx130is (1.01d & 1.01f)
  ixus310hs (1.00a & 1.01a)
  sx40hs (1.00d, 1.00g & 1.00i)
  g1x (1.00e, 1.00f & 1.00g)
  g5x (1.00c, 1.01a, 1.01b)
  g7x2 (1.01a, 1.01b, 1.10b)

 

Related Topics