我有一个小问题,我找不到解决方案!
我的代码是(这只是一个示例代码,但我的原始代码执行类似的操作):
float x = [@"2.45" floatValue];
for(int i=0; i<100; i++)
x += 0.22;
NSLog(@"%f", x);
Run Code Online (Sandbox Code Playgroud)
输出是52.450001而不是52.450000!
我不知道因为这种情况发生了!
谢谢你的帮助!
〜〜解决
谢谢大家!是的,我用双重型解决了!
根据这篇文章,当比较float和double时,float应被视为double.以下程序似乎没有遵循此声明.这种行为看起来非常不可预测.这是我的计划:
void main(void)
{
double a = 1.1; // 1.5
float b = 1.1; // 1.5
printf("%X %X\n", a, b);
if ( a == b)
cout << "success " <<endl;
else
cout << "fail" <<endl;
}
Run Code Online (Sandbox Code Playgroud)
我还打印了值的十六进制表示法.它们在两种情况下都不同.我的编译器是Visual Studio 2005
你能解释一下这个输出吗?谢谢.
请看下面的代码:
float num = 72000000.69f;
System.out.println(String.format("%.2f", num));
Run Code Online (Sandbox Code Playgroud)
输出:72000000.00
我失去了精确度.我试过将float转换为BigDecimal,然后打印值,但它没有帮助.
有什么方法可以保持精度?
我正在开发一个使用NI-DAQ的应用程序,以下是提供商提供的一些方法.
void someMethod(Calibration *cal, float myArray[], float result[])
{
newMethod(&cal->rt,myArray,result,cal->cfg.TempCompEnabled);
}
void newMethod(RTCoefs *coefs, double myArray[],float result[],BOOL tempcomp)
{
float newMyArray[6];
unsigned short i;
for (i=0; i < 6; i++)
{
newMyArray[i]=myArray[i];
}
}
Run Code Online (Sandbox Code Playgroud)
我基本上调用someMethod(),为myArray []和result []提供一个包含六个元素([6])的数组.正如你在代码中看到的那样,之后调用newMethod(),并将float myArray [6]传递给double myArray []参数(我真的不明白为什么这段代码的开发人员选择使用双数组,因为在newMethod()中声明的唯一数组是float类型.
现在我的问题出现了:在for循环中,一些值没有任何问题,但是当第四个和第五个值传递给newMyArray []时,它会收到两个值的"-1.#INF0000".乍一看,我认为这将是一些垃圾值,但每次执行时都会出现"-1.#INF0000".
我知道C语言有时会很棘手,但我真的不知道为什么会这样......
我遇到过这个向量/标量划分实现:
public static Vector2 operator /(Vector2 value1, float divider)
{
float factor = 1 / divider;
value1.X *= factor;
value1.Y *= factor;
return value1;
}
Run Code Online (Sandbox Code Playgroud)
我尝试通过简单的divison实现它:
public static Vector2 operator /(Vector2 value1, float divider)
{
return new Vector2(value1.X / divider, value1.Y / divider);
}
Run Code Online (Sandbox Code Playgroud)
我尝试运行模拟,结果似乎略有不同.
这是一种提高计算精度的技巧吗?
我有这个代码:
double timeTillTick = 15.0;
private void lower_Tick(object sender, EventArgs e)
{
if (timeTillClear > 0)
{
timeTillClear -= 0.1;
clearingIn10SecondsToolStripMenuItem.Text = "Clearing in " + timeTillClear + " seconds.";
}
else
{
lower.Enabled = false;
}
}
Run Code Online (Sandbox Code Playgroud)
每100毫秒降低一次滴答.当它:
它增加了0.000000000000001.为什么?
所有在标题:确实IEEE-754 float,double并quad保证准确表示-2,-1,-0,0,1,2?
我有一个python十进制(货币金额),我想要舍入到两位小数.我尝试使用常规round()函数执行此操作.不幸的是,这会返回一个浮点数,这使得继续执行以下操作变得不可靠:
>>> from decimal import Decimal
>>> a = Decimal('1.23456789')
>>> type(round(a, 2))
<type 'float'>
Run Code Online (Sandbox Code Playgroud)
在十进制模块中,我看到了与舍入有关的一些事情:
我认为这些都没有实际给出我想要的东西(或者我错了吗?).
所以我的问题:有没有人知道我如何可靠地将Python Decimal舍入到2位小数,这样我才能继续使用Decimal?欢迎所有提示!
python floating-point decimal rounding floating-point-precision
我的BinvA矩阵的(1,1)条目得到一个非常奇怪的值我
只是试图反转B矩阵并做一个(B ^ -1)A乘法.
我明白,当我手工计算时,我的(1,1)应该是0,但我得到的是1.11022302e-16.我该如何解决?我知道浮点数不能完全准确地表示,但为什么这给我这样一个不准确的响应而不是四舍五入到有没有办法让它更精确?
她是我的代码:
import numpy as np
A = np.array([[2,2],[4,-1]],np.int)
A = A.transpose()
B = np.array([[1,3],[-1,-1]],np.int)
B = B.transpose()
Binv = np.linalg.inv(B) #calculate the inverse
BinvA = np.dot(Binv,A)
print(BinvA)
Run Code Online (Sandbox Code Playgroud)
我的打印声明:
[[ 1.11022302e-16 -2.50000000e+00]
[ -2.00000000e+00 -6.50000000e+00]]
Run Code Online (Sandbox Code Playgroud) python floating-point numpy matrix-multiplication floating-point-precision
我需要将UInt32类型转换为float而不进行舍入.说我同意
float num = 4278190335;
uint num1 = num;
Run Code Online (Sandbox Code Playgroud)
该值立即变为4278190336.这有什么办法吗?