[SOLVED] How to write a negative float?

Can someone please help me to understand this problem:

I’m trying to assign a negative floating point value, but it seems like I’m not getting the right syntax.

This one works:

val float offTemp = -1       // Temperature offset

rule "some rule"
	Item LivingRoomThermostat_SetTemperature changed
then
	newTemp = Float::parseFloat(LivingRoomThermostat_SetTemperature.state.toString)
	newTemp = newTemp + offTemp
end

and this one works (watch the offTemp value):

val float offTemp = 1.0       // Temperature offset

rule "some rule"
	Item LivingRoomThermostat_SetTemperature changed
then
	newTemp = Float::parseFloat(LivingRoomThermostat_SetTemperature.state.toString)
	newTemp = newTemp + offTemp
end

but this fails (again, watch the offTemp value):

val float offTemp = -1.0       // Temperature offset

rule "some rule"
	Item LivingRoomThermostat_SetTemperature changed
then
	newTemp = Float::parseFloat(LivingRoomThermostat_SetTemperature.state.toString)
	newTemp = newTemp + offTemp
end

and results in

[ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'some rule': An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.FloatExtensions.operator_plus(float,float) on instance: null

It looks to me as if the value -1.0 is not accepted, therefore the value is null. But how to write a negative floating point value? Where’s my mistake?

I’ve run into a problem something like that, can’t remeber the cicumstance but the rules parser obviously struggled with the hyphen/minus. I circumvented by forcing it to understand “number” context before encountering the minus i.e. embedding it in an evaluation.
var whatever = 0 - 1

Use the Number type instead of Float

val Number offTemp = -1.0       // Temperature offset
var Number newTemp = 0

rule "some rule"
	Item LivingRoomThermostat_SetTemperature changed
then
	newTemp = LivingRoomThermostat_SetTemperature.state as Number
	newTemp = newTemp + offTemp
end
1 Like

Thanks @vzorglub, that works perfectly and is much more elegant as well.

EDIT: So float seems partly buggy and should be avoided.

Primitive types should be avoided