This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
fileinster's profile picture

Writing raw integers is producing floats

Started by fileinster, 10 October 2014 - 09:37 PM
fileinster #1
Posted 10 October 2014 - 11:37 PM
Using CC 1.5

I have code that writes a number to a monitor, example:

mon = peripheral.wrap("top")
i=1
mon.write(i)

However, instead of writing "1" it insists on writing "1.0"

On playing about on the interactive Lua prompt I have found that a raw write seems to perform as expected.

lua> term.write(i) -> 1.0
lua> write(i) -> 1


With that said I have found a workaround that seems to work, but the nature of this seems odd:
term.write(i.."") -> 1


Can someone make sense of this for me, and educate me how to write integers correctly. PS, I have also tried math.floor with no effect.



TIA
Dog #2
Posted 10 October 2014 - 11:57 PM
When writing a number to the screen you can use tostring() to eliminate the .0

term.write(tostring(i))

Is that what you were looking for?
fileinster #3
Posted 11 October 2014 - 01:08 AM
Again, that to me is another workaround. I want to be educated why an integer is printing as a float.
Cranium #4
Posted 11 October 2014 - 01:09 AM
The write functions automatically use tostring() on numbers before writing. There should be no reason the steps you provided would write the output you're saying. That's not even standard Lua operation.
fileinster #5
Posted 11 October 2014 - 01:18 AM
Now, that's an education!

I knew the problem was my understanding. Whether standard or not it does still seem strange… when is an integer not an integer!?


Thanks again.
Lyqyd #6
Posted 11 October 2014 - 01:22 AM
There aren't integers or floats in Lua, only "numbers". Passing the number type through LuaJ to the Java side may result in all of them coming in with the same type. IIRC, peripherals get them as Doubles, never integers or floats.
fileinster #7
Posted 11 October 2014 - 01:36 AM
Excellent!
That's in depth and explains it fully. Very hard to find nuances like this in the documentation.