如果您正在阅读有关 20 世纪 90 年代图形学发展的新闻,您可能会关注 Jim Blinn 在IEEE 计算机图形与应用中的专栏“Jim Blinn 的角落”。1997 年夏天,您可能会在名为“浮点技巧”的专栏中打开您的问题。所有的技巧都基于 Blinn 从微软的两位老手 Steve Gabriel 和 Gideon Yuval 那里传授的以下知识:
\n“如果只处理正数,则将浮点数的位模式解释为整数,给出对数函数的分段线性近似”
\n我的问题是,浮点数是什么导致了这个特性?
\n\n// These will result in undefined behavior\n// and are not the \'right\' way to do this\n// but I am following Blinn 1997 here\nint AsInteger(float f) {\n return * ( int * ) &f;\n}\n\nfloat AsFloat(int i) {\n return * ( float * ) &i;\n}\n\n// FISR demonstrating the logaritmic …Run Code Online (Sandbox Code Playgroud) 我想知道你是否可以帮助解释将整数转换为float或float转换为整数的过程.对于我的课程,我们只使用按位运算符来完成此操作,但我认为从类型到类型的强制理解将在这个阶段帮助我更多.
根据我目前所知,对于int要浮动,你必须将整数转换为二进制,通过查找有效数,指数和分数来规范化整数的值,然后从那里输出浮点值?
至于float到int,你必须将值分成有效数,指数和分数,然后反转上面的指令得到一个int值?
我试着按照这个问题的说明:在C中将float转换为int(按位)
但我真的不能理解它.
另外,有人可以解释为什么在将int转换为float时大于23位的值需要舍入?
提前致谢
现在,这是我应该实现的函数的函数头:
/*
* float_from_int - Return bit-level equivalent of expression (float) x
* Result is returned as unsigned int, but
* it is to be interpreted as the bit-level representation of a
* single-precision floating point values.
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 30
* Rating: 4
*/
unsigned float_from_int(int x) {
...
}
Run Code Online (Sandbox Code Playgroud)
我们不允许进行浮动操作或任何类型的铸造.
现在我尝试实现在这个站点给出的第一个算法:http://locklessinc.com/articles/i2f/
这是我的代码:
unsigned float_from_int(int x) {
// grab sign bit
int xIsNegative = …Run Code Online (Sandbox Code Playgroud) 如何将int转换为float(返回为无符号,但解释为float)?我不能使用任何高级语言结构,如联合,只有按位操作,控制逻辑(if/while),+和 - 等.