- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Zarith库来做任意精度的有理算术。假设我有一个有理数 q
类型 Q.t
这是两个大整数的比率(Q
是 Zarith 的任意精度有理数模块)。有时,为了便于阅读,我想将此数字打印为浮点数,有时我需要将此数字转换为浮点数,以便以后进行非任意精度计算。有没有办法转换q
到一定精度的浮点数?
我转换的方式q
to floating-point 现在没有任何保证,并且可以创建未定义的浮点数(Z
是任意精度整数模块):
let to_float q =
let n, d = num q, den q in
(* check if d is zero and raise an error if it is *)
let nf, df = Z.to_float n, Z.to_float d in
nf /. df
q
的浮点数?
let to_float q =
let n, d = num q, den q in
let n_sign = Z.sign n in
let d_sign = Z.sign d in (* always >= 0 *)
if d_sign = 0 then raise Division_by_zero;
let n = Z.abs n in
if n_sign = 0 then 0. else
let shift = (Z.numbits n) - (Z.numbits d) - 55 in
let is_subnormal = shift < -1076 in
let shift = if is_subnormal then -1076 else shift in
let d = if shift >= 0 then Z.shift_left d shift else d in
let n = if shift < 0 then Z.shift_left n (-shift)
else n in
let quotient, remainder = Z.div_rem n d in
let quotient = if (Z.compare remainder (Z.zero)) = 0 && Z.is_even quotient then
Z.add Z.one quotient else quotient in
let quotient = if not is_subnormal then quotient else
let round_select = Z.to_int @@ Z.rem quotient @@ Z.of_int 8 in
Z.add quotient [|Z.zero;Z.minus_one;Z.of_int (-2);Z.one;Z.zero
;Z.minus_one;Z.of_int 2;Z.one|].(round_select)
in
let unsigned_res = ldexp (Z.to_float quotient) shift in
if n_sign = 1 then unsigned_res else -.unsigned_res
mpq_get_d
编写一个接口(interface)。稍后运行,但我不完全确定如何做到这一点。我看到的唯一方法是转换
q : Q.t
到一个字符串并传递
to:
int mpq_set_str (mpq_t rop, const char *str, int base)
rop
至
mpq_get_d
在 OCaml 中或有描述如何执行此操作的引用?我浏览了
chapter 19 of RWO并没有看到这样的情况。
最佳答案
如果您有权访问
log2
操作和 n > 0
, d > 0
;过滤掉明显的下溢/溢出shift
这样2^-shift*n/d
介于 2^54
之间和 2^56
. x = 2^-shift*n/d
, 使用 round-to-odd 四舍五入到最接近的整数舍入法。 x
到最接近的 IEEE 754 double 值 dx
, 使用通常的舍入到偶数舍入模式。 ldexp(dx, shift)
. shift
值很容易检测到这些情况。以下。
from math import ldexp
def to_float(numerator, denominator):
"""
Convert numerator / denominator to float, correctly rounded.
For simplicity, assume both inputs are positive.
"""
# Shift satisfies 2**54 < (numerator / denominator) / 2**shift < 2**56
shift = numerator.bit_length() - denominator.bit_length() - 55
# Divide the fraction by 2**shift.
if shift >= 0:
denominator <<= shift
else:
numerator <<= -shift
# Convert to the nearest integer, using round-to-odd.
q, r = divmod(numerator, denominator)
if r != 0 and q % 2 == 0:
q += 1
# Now convert to the nearest float and shift back.
return ldexp(float(q), shift)
bit_length
正整数上的方法 n
给出表示 n
所需的位数,或者换句话说 1 + floor(log2(n))
. divmod
是一个 Python 函数,它同时计算整数除法的商和余数。 q
(很容易)适合 64 位整数 numerator / denominator
时到最接近的整数,并在将该整数舍入为浮点数时再次。第一轮使用round-to-odd方法;这确保了第二轮(隐含在从 int 到 float 的转换中)给出的结果与我们将分数直接四舍五入为浮点数相同。 ldexp
操作可能会引入第三次舍入。有可能处理这个问题,但要小心。请参阅下面的一些代码。 long_true_divide
开头的评论函数概述了该方法。
def to_float(numerator, denominator):
"""
Convert numerator / denominator to float, correctly rounded.
For simplicity, assume both inputs are positive.
"""
# Choose shift so that 2**54 < numerator / denominator / 2**shift < 2**56
shift = numerator.bit_length() - denominator.bit_length() - 55
# The 'treat_as_subnormal' flag catches all cases of subnormal results,
# along with some cases where the result is not subnormal but *is* still
# smaller than 2**-1021. In all these cases, it's sufficient to find the
# closest integer multiple of 2**-1074. We first round to the nearest
# multiple of 2**-1076 using round-to-odd.
treat_as_subnormal = shift < -1076
if treat_as_subnormal:
shift = -1076
# Divide the fraction by 2**shift.
if shift >= 0:
denominator <<= shift
else:
numerator <<= -shift
# Convert to the nearest integer, using round-to-odd.
q, r = divmod(numerator, denominator)
if r != 0 and q % 2 == 0:
q += 1
# Now convert to the nearest float and shift back.
if treat_as_subnormal:
# Round to the nearest multiple of 4, rounding ties to
# the nearest multiple of 8. This avoids double rounding
# from the ldexp call below.
q += [0, -1, -2, 1, 0, -1, 2, 1][q%8]
return ldexp(float(q), shift)
关于floating-point - 将任意精度的有理数(OCaml,zarith)转换为近似 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33623875/
为什么这不返回每个社区(边界框)中的点数? import geopandas as gpd def radius(points_neighbour, points_center, new_field_
嘿! 我有一张图片,我想在该图片上选择一个点并告诉它应该变换到哪个坐标。我想为一些数字点做这个。当我完成时,整个图像会发生变化,因此会考虑局部性。 最重要的是,我可以选择任意多的点,并且所选的点会转换
我有代码: class Point3D{ protected: float x; float y; float z; public:
我正在开发我的第一个 Spring Boot + Spring Data JPA + Hibernate 5,在 PostgreSQL 上工作数据库。 我在尝试映射具有 point 作为数据类型的字段
当我尝试编译这个简单的代码时,我在构造函数中遇到了两个错误:“类型值不能用作默认参数”我该如何解决这个问题? public class PointerArgs { public P
当我尝试编译这个简单的代码时,我在构造函数中遇到了两个错误:“类型值不能用作默认参数”我该如何解决这个问题? public class PointerArgs { public P
目前我正在实现一项提供集体旅行的交通服务,但我遇到了一个问题: 假设我在下图中得到了点 G = {A,B,C,D,F,R,W} =>。 当用户选择 from(A) -> to(W) 时,它们之间有点:
我有一个名为 Shop 的实体,它有一个名为 Position 的 DBGeorgpraphy 列 数据库中的示例商店的位置值为 POINT (145.034242 -37.825519) 我正在尝试
我看了几个类似的帖子,但我要么不明白他们提供的是什么,要么他们似乎不适用。我是新来的,我会尽力遵守规则。 我们在类(class)的最后 2 周学习 c++,期末学习 40 小时 :),所以我是初学者。
我正在使用 tf2 将点从源帧转换为目标帧。下面是代码片段: import tf2_ros import tf2_geometry_msgs transform = tf_buffer.lookup_
我需要找到一种算法,根据给定的一组大小为 n 的点 S 计算凸包。我知道 S 正好有 6 个点 构成了凸包。 最好和最有效的方法是什么? 我想从 S 生成所有可能的点组合(这将是 n 选择 6 个点)
我有一个在屏幕坐标中的 CGPoint。我还有一个应用了变换矩阵(缩放、旋转和平移)的 CALayer。 如何将屏幕坐标中的点转换为图层的局部坐标? 最佳答案 CALayer 有执行此操作的方法,请在
我正在创建自定义控件,它将从点列表(或数组)中绘制形状。我已经完成了基本的绘图功能,但现在我正在为 Visual Studio 中的设计时支持而苦苦挣扎。 我创建了两个属性: private Poin
此函数是从“JavaScript:权威指南”复制的,但由于某种原因它不起作用... **points.dist = function () { ^ ReferenceError: 点未定义**我对此很
我有一个像这样的自定义适配器: private List items = new ArrayList<>(); private Context context; public UserSpinnerA
代码: UPDATE tbl_name SET points = points + 1 WHERE 'GAME 1' LIKE "%Vikes%" GAME 1 列包含包含 Vikes
我有一个点。我正在尝试将 x 作为 int。如果我使用 Point.x,我将得到 x 作为 int。但我的印象是我应该尽可能使用 setter/getter ( Why use getters and
我正在开发一个小型信誉系统,但遇到了一个问题。 因此,在我的示例中,我想为 4 种不同类型的用户创建一个图片网站;我们称他们为:业余、好、非常好、专业。 每个用户可以上传一张图片,这张图片可以被其他用
我有一个关于事件形状模型的问题。我正在使用 T. Coots 的论文(可以找到 here 。) 我已经完成了所有初始步骤(Procrustes 分析计算平均形状,PCA 减少尺寸)但仍停留在拟合上。
Android moving Image one point (0,0) to another point (30,400). using animation or normal looping co
我是一名优秀的程序员,十分优秀!