“变量转换”的版本间差异

来自Shiyin's note
跳到导航 跳到搜索
无编辑摘要
 
(未显示2个用户的5个中间版本)
第4行: 第4行:
IDL> 5, 6
IDL> 5, 6
*ceil:向上进
*ceil:向上进
IDL>print,round(5.1),round(5.9)
IDL>print,ceil(5.1),ceil(5.9)
IDL> 6, 6
IDL> 6, 6
*floor:截取整数
*floor:截取整数
IDL>print,round(5.1),round(5.9)
IDL>print,floor(5.1),floor(5.9)
IDL> 5, 5
IDL> 5, 5


*整数范围很小(long),要转换大的数,加上/L64
*结果是long型的整数要转换大的数,加上/L64
*[[astro_lib]]中的nint程序,可以代替round,而且
*[[astro_lib]]中的nint程序,可以代替round,而且
:(1) if no absolute value exceeds 32767, then the array is returned as
:(1) if no absolute value exceeds 32767, then the array is returned as a type INTEGER instead of LONG
:(2) NINT will work on strings, e.g. print,nint(['3.4','-0.9']) will give [3,-1], whereas ROUND() gives an error message
as a type INTEGER instead of LONG

:(2) NINT will work on strings, e.g. print,nint(['3.4','-0.9']) will
python:
give [3,-1], whereas ROUND() gives an error message
*分页/补齐 ceil 确保余量能覆盖,不丢数据
*上限控制 floor 不超过上限,安全保守
*数组索引 trunc 或 int 始终向 0 靠拢,避免越界
*金融精度 decimal 模块 可精确选择舍入策略,避免浮点误差
===round (python)===
* 它本质上是“凑近 10^(-n) 的倍数”;
* 对于 .5 这种中点,Python 用的是银行家舍入,而不是传统的“四舍五入”;
* 如果需要传统舍入,可以自己写“远离 0”的函数;
* 在工程实践中,不同场景应该用不同的取整方法。
round(2.5) # 2
round(3.5) # 4
round(1.25, 1) # 1.2
round(1.35, 1) # 1.4
round(25, -1) # 20
round(35, -1) # 40







==消除变量==
*temporary(var)

2025年9月10日 (三) 23:56的最新版本

浮点数转成整数

  • round:四舍五入
IDL>print,round(5.1),round(5.9)
IDL> 5, 6
  • ceil:向上进
IDL>print,ceil(5.1),ceil(5.9)
IDL> 6, 6
  • floor:截取整数
IDL>print,floor(5.1),floor(5.9)
IDL> 5, 5
  • 结果是long型的整数,要转换更大的数,加上/L64
  • astro_lib中的nint程序,可以代替round,而且
(1) if no absolute value exceeds 32767, then the array is returned as a type INTEGER instead of LONG
(2) NINT will work on strings, e.g. print,nint(['3.4','-0.9']) will give [3,-1], whereas ROUND() gives an error message

python:

  • 分页/补齐 ceil 确保余量能覆盖,不丢数据
  • 上限控制 floor 不超过上限,安全保守
  • 数组索引 trunc 或 int 始终向 0 靠拢,避免越界
  • 金融精度 decimal 模块 可精确选择舍入策略,避免浮点误差

round (python)

  • 它本质上是“凑近 10^(-n) 的倍数”;
  • 对于 .5 这种中点,Python 用的是银行家舍入,而不是传统的“四舍五入”;
  • 如果需要传统舍入,可以自己写“远离 0”的函数;
  • 在工程实践中,不同场景应该用不同的取整方法。
round(2.5)      # 2
round(3.5)      # 4
round(1.25, 1)  # 1.2
round(1.35, 1)  # 1.4
round(25, -1)   # 20
round(35, -1)   # 40




消除变量

  • temporary(var)