小编Iho*_*alo的帖子

为什么BigFloat.to_s不够精确?

我不确定这是不是一个bug.但我一直在玩,big我不明白为什么这段代码以这种方式工作:

https://carc.in/#/r/2w96

require "big"

x = BigInt.new(1<<30) * (1<<30) * (1<<30)
puts "BigInt: #{x}"

x = BigFloat.new(1<<30) * (1<<30) * (1<<30) 
puts "BigFloat: #{x}"
puts "BigInt from BigFloat: #{x.to_big_i}"
Run Code Online (Sandbox Code Playgroud)

产量

BigInt: 1237940039285380274899124224
BigFloat: 1237940039285380274900000000
BigInt from BigFloat: 1237940039285380274899124224
Run Code Online (Sandbox Code Playgroud)

首先,我认为BigFloat需要更改 BigFloat.default_precision为更大的数字.但是从这段代码看起来它只在尝试输出#to_s值时才有意义.

与BigFloat的精度设置为1024(https://carc.in/#/r/2w98)相同:

产量

BigInt: 1237940039285380274899124224
BigFloat: 1237940039285380274899124224
BigInt from BigFloat: 1237940039285380274899124224
Run Code Online (Sandbox Code Playgroud)

BigFloat.to_s用途LibGMP.mpf_get_str(nil, out expptr, 10, 0, self).GMP在哪里说:

mpf_get_str (char *str, mp_exp_t *expptr, int base, size_t n_digits, const mpf_t op)

将op转换为基数中的数字字符串.基本参数可以在2到62之间或从-2到-36之间变化.最多将生成n_digits数字.不返回尾随零.不会产生比op准确表示的数字更多的数字. …

gmp crystal-lang

7
推荐指数
1
解决办法
107
查看次数

滑轨。为什么要使用ActiveRecord?

我试图做简单的SQL查询任务。因此,我在自己的开发环境中使用了Active Record和SQLite,而在我的产品中使用了PostgreSQL。环境

我认为使用ActiveRecord是因为它可以根据所使用的数据库生成查询,但是我的所有查询对于PostgreSQL都有一些错误。

查询:

@sql[0] = Task.select(:done, :deadline).order(name: :asc).distinct

@sql[1] = Task.joins(:project).group(:project_id).select("projects.name, 
COUNT(*) as TaskCount").order("TaskCount DESC")

@sql[2] = Task.joins(:project).group(:project_id).select("projects.name, 
COUNT(*) as TaskCount").order("projects.name ASC")

@sql[3] = Task.select("projects.name AS pName","tasks.*")
.joins(:project).where("projects.name LIKE ?",'N%')
          .where("projects.name LIKE ?","%_a_%")'

@sql[4] = Project.joins("LEFT OUTER JOIN tasks 
ON 'projects'.'id'='tasks'.'project_id'")
.group(:project_id)
.select("projects.*, COUNT(tasks.project_id) as TaskCount")
          .where("projects.name LIKE ?","%_a_%")

@sql[5] = Task.group(:name).having("COUNT(*)>1").order(name: :asc)

@sql[6] = Task.joins(:project).where("projects.name = 'Garage'")
.group("tasks.name, tasks.done, tasks.deadline")
.having("COUNT(*)>1").select("tasks.*, COUNT(*)").order("COUNT(*) DESC")

@sql[7] = Task.where("tasks.done = ?",true).joins(:project).group(:project_id)
.having("COUNT(*)>=10").select("projects.name, COUNT(*) as TaskCount")
.order("projects.id DESC")
Run Code Online (Sandbox Code Playgroud)

他们每个人都有一些错误。

我不希望您解决它们

  • 我的问题是我如何首先避免它们? …

sql postgresql activerecord ruby-on-rails ruby-on-rails-4

4
推荐指数
1
解决办法
298
查看次数