我想做一件简单的事情:
can not use score1 * 0 + score2 * 0 + score3 * 0 + score4 * 0 (type int16) as type float64 in assignment
但是这报告错误:
function result_selected_abc_data()
{
$this->auth->restrict();
$this->load->model('usermodel');
$this->load->model('productmodel');
$level=$this->session->userdata('level');
$this->load->library('form_validation');
$this->form_validation->set_rules('date1','Tanggal Di Masukkan','trim|required');
$this->form_validation->set_rules('date2','Tanggal Belum Di Masukkan','trim|required');
$this->form_validation->set_error_delimiters('<span style="color:#FF0000;">','</span>');
if($this->form_validation->run() == false){
$data['parent']=$this->usermodel->get_data_parent_menu($level);
$data['first_child'] = $this->usermodel->get_data_first_child_menu($level);
$data['second_child'] = $this->usermodel->get_data_second_child_menu($level);
$data['third_child'] = $this->usermodel->get_data_third_child_menu($level);
$data['fourth_child'] = $this->usermodel->get_data_fourth_child_menu($level);
$data['fifth_child'] = $this->usermodel->get_data_fifth_child_menu($level);
$data['sixth_child'] = $this->usermodel->get_data_sixth_child_menu($level);
$data['seventh_child'] = $this->usermodel->get_data_seventh_child_menu($level);
$this->template->display('ABC_select_date',$data);
}
else
{
$data['selected_date']= array(
'date1' => $this->input->post('date1'),
'date2' => $this->input->post('date2')
);
$date1=$this->input->post('date1');
$date2=$this->input->post('date2');
$data['ABC_report_result']= $this->productmodel->get_ABC_data_based_on_date($date1,$date2);
$this->productmodel->save_abc_data($data['ABC_report_result']);
$this->template->display('ABC_select_data_result',$data);
}
}
我该怎么做?
答案 0 :(得分:3)
Go不提供隐式数字转换,请参阅此常见问题解答:Why does Go not provide implicit numeric conversion?。
这意味着在进行算术运算时,您不能混合使用不同的类型。它们必须属于同一类型。 Spec:
您的情况略有不同,因为0.25
是untyped constant,但由于它有一小部分,因此无法将其转换/解释为int16
,因此您可以获得编译时间错误。来自spec:
如果常量值不能表示为相应类型的值,则会出错。
在这种情况下,您有3个选项:
将分数明确转换为float64
:
res = float64(score1) * 0.25 + float64(score2) * 0.25 +
float64(score3) * 0.25 + float64(score4) * 0.25
对float64
变量使用score
类型。
var score1 float64 = 500
var score2 float64 = 400
// ...
由于您的算法计算平均值,您只需执行以下操作:
res = float64(score1 + score2 + score3 + score4) / 4
答案 1 :(得分:1)
您的常量(0.25)被截断为整数(0)。
两种解决方法:
将score1等变量强制转换为float32:
var score1 int16 = 500
var score2 int16 = 400
var score3 int16 = 300
var score4 int16 = 200
res := float32(score1)*0.25 + float32(score2)*0.25 + float32(score3)*0.25 + float32(score4)*0.25
fmt.Println("Score", res)
或者更明智,而不是将它们声明为int16,将它们声明为float32以开头:
var score1a float32 = 500
var score2a float32 = 400
var score3a float32 = 300
var score4a float32 = 200
res2 := score1a * 0.25 + score2a * 0.25 + score3a * 0.25 + score4a * 0.25
fmt.Println("Result 1", res)
fmt.Println("Result 2", res2)
上
答案 2 :(得分:0)
在go中没有自动类型推广,您需要包含显式转换。
我写的代码是这样的:
package main
import "fmt"
func getScore() float64 {
s1, s2, s3, s4 := int16(500), int16(400), int16(300), int16(200)
return (float64(s1) + float64(s2) + float64(s3) + float64(s4)) * 0.25
}
func main() {
fmt.Println(getScore())
}