Rounding function - am I using it correctly

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
paul402
Posts: 105
Joined: Monday 23 May 2016 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Rounding function - am I using it correctly

Post by paul402 »

After the start help here I am making progress on my project - thanks.

This line works perfectly but has a lot of trailing digits which I want to round to 2 digits.

Code: Select all

local inside_AH=(((0.000002*(inside_temp*inside_temp*inside_temp*inside_temp))+(0.0002*(inside_temp*inside_temp*inside_temp))+(0.0095*(inside_temp*inside_temp))+(0.337*inside_temp)+4.9034)*inside_rh)/100
	        
So I add "round(number, decimalPlaces)" as the manual describes ...

Code: Select all

local inside_AH=round((((0.000002*(inside_temp*inside_temp*inside_temp*inside_temp))+(0.0002*(inside_temp*inside_temp*inside_temp))+(0.0095*(inside_temp*inside_temp))+(0.337*inside_temp)+4.9034)*inside_rh)/100,2)
and have this which gives me an error "lua:21: attempt to call global 'round' (a nil value)"

What obvious mistake have I made please? I can't see it or I don't understand how to use it properly :(
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Rounding function - am I using it correctly

Post by emme »

round is not a built in function.... you have to define it everytime:

Code: Select all

function round(num, numDecimalPlaces)
  local mult = 10^(numDecimalPlaces or 0)
  return math.floor(num * mult + 0.5) / mult
end
The most dangerous phrase in any language is:
"We always done this way"
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Rounding function - am I using it correctly

Post by dannybloe »

It is a built-in function but you have to call it like:

Code: Select all

domoticz.round(number, decimalPlaces)
See Domoticz attributes and methods
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
paul402
Posts: 105
Joined: Monday 23 May 2016 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Rounding function - am I using it correctly

Post by paul402 »

OK dannybloe if it is built in then this didn't work

Code: Select all

inside_AH=domoticz.round((((0.000002*(inside_temp*inside_temp*inside_temp*inside_temp))+(0.0002*(inside_temp*inside_temp*inside_temp))+(0.0095*(inside_temp*inside_temp))+(0.337*inside_temp)+4.9034)*inside_rh)/100,2)
and neither did this!

Code: Select all

local inside_AH=domoticz.round((((0.000002*(inside_temp*inside_temp*inside_temp*inside_temp))+(0.0002*(inside_temp*inside_temp*inside_temp))+(0.0095*(inside_temp*inside_temp))+(0.337*inside_temp)+4.9034)*inside_rh)/100,2)
What do I have to change in my script? I am baffled! Actually more than baffled. I'm going to drink a whisky and relax :)
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Rounding function - am I using it correctly

Post by dannybloe »

I would first do the calculation and then feed it to the round function. You can print the unrounded value to the log. Have you tried to round a simple value like 1.5?
Make sure you use dzVents 2.3.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
paul402
Posts: 105
Joined: Monday 23 May 2016 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Rounding function - am I using it correctly

Post by paul402 »

The round function is not available yet for the version I am using so I used this math.floor function which works perfectly for my needs.

Code: Select all

inside_AH=math.floor(inside_AH*100)/100
Thanks again for the help
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Rounding function - am I using it correctly

Post by dannybloe »

This is the function that's built in dzVents:

Code: Select all

	local round = function(x, n)
		n = math.pow(10, n or 0)
		x = x * n
		if x >= 0 then
			x = math.floor(x + 0.5)
		else
			x = math.ceil(x - 0.5)
		end
		return x / n
	end
I doubt your function does anything at all. When I try it in Lua it doesn't do anything at all.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest