I am building a program on solidity that receives 3 values(bonus, value1, value2) through function, Then, another value called priority is calculated as follows: Priority= (0.5*Value1 + 0.5*value2) / bonus
. However, when I code this function in remix, I got errors. Is there any way to fix this and I still can compute the "Priority" on solidity?
pragma solidity ^0.4.24;
contract Test{
struct student{
uint bonus;
uint value1;
uint value2;
uint Priority; //priority of student
}
mapping(address => student) students ; //etherum does not allow
declaration of a function of type mapping so we define array of addresses
address [] public student_list; // array of all student addresses
function Recieve_Request(address _address,uint _bonus, uint _value1, uint
_value2) public {
var student = students[_address]; //instance of struct student and map it to
students mapping
student.bonus=_bonus;
student.value1=_value1;
student.value2=_value2;
student.Priority=(0.5 * _value1 + 0.5 * _value2)/ _bonus;
student_list.push(_address) -1;
}
function get_ESU(address ins) view public returns (uint, uint, uint) {
return (students[ins].value1, students[ins].value2,
students[ins].Priority);
}
}
Best Answer
The problem is simply that ‘0.5’ do not exist in integer math. The minimum positive number is ‘1’.
You can divide by 2 instead of multiplying by 0.5.