最近很多小伙伴都在问c–将int32重新解释为float和c语言把int改成float这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展.net–为什么将Int32转换为Float6
最近很多小伙伴都在问c – 将int32重新解释为float和c语言把int改成float这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展.net – 为什么将Int32转换为Float64会导致数据发生变化?、Angular 8.3.2:将ArrayBuffer转换为Float32Array、assembly – 使用Bitwise将Int转换为Float/Float到Int、assembly – 将uint32的向量转换为float向量的最有效方法?等相关知识,下面开始了哦!
本文目录一览:- c – 将int32重新解释为float(c语言把int改成float)
- .net – 为什么将Int32转换为Float64会导致数据发生变化?
- Angular 8.3.2:将ArrayBuffer转换为Float32Array
- assembly – 使用Bitwise将Int转换为Float/Float到Int
- assembly – 将uint32的向量转换为float向量的最有效方法?
c – 将int32重新解释为float(c语言把int改成float)
在reinterpretedFloat函数中,我创建了一个样本整数并将内存复制到float变量.
当memcpy-ed float返回时,值就会发生变化.
这是示例代码.
#include <stdio.h> #include <stdint.h> void printHex(const unsigned char* buff,int count) { printf("0X"); for (int i = 0; i < count; ++i) { printf("\t%X",buff[i]); } printf("\n"); } float reinterpretedFloat() { int32_t value = 0x7F845E58; float tmp; memcpy(&tmp,&value,sizeof(float)); printHex(reinterpret_cast<const unsigned char*>(&tmp),4); //memcpy return tmp; } int main() { float newFloat = reinterpretedFloat(); printHex(reinterpret_cast<const unsigned char*>(&newFloat),4); //returned value return 0; }
结果就是这样.
0X 58 5E 84 7F(memcpy) 0X 58 5E C4 7F(returned value)
我的预期是0X 58 5E 84 7F ……
任何机构都可以解释为什么会这样?
在x64配置中,这不会发生.
解决方法
x86-64和32位x86结果之间的区别是因为在前一种模式下,要从reinterpretedFloat()函数返回浮点值,来自SSE ISA扩展的MOVSS指令将值加载到xmm0寄存器中而不进行任何转换,而在后者使用FLD dword […],它将32位浮点数转换为x87内部80位长双精度格式,将信号状态归一化为quiet *.
机制上的差异是由于x86-64体系结构保证支持SSE,因此ABI使用它,而i386 ABI不需要它,因为并非所有x86 cpu都支持它.
*从技术上讲,转换会导致无效操作异常,但由于它被屏蔽(默认情况下),因此您可以获得从NaN中删除信令状态的规范化结果
.net – 为什么将Int32转换为Float64会导致数据发生变化?
.assembly variables {} .method public static void main() cil managed { .entrypoint .maxstack 8 .locals init (float64) ldstr "Enter a digit: " call void [mscorlib]System.Console::WriteLine(string) call int32 [mscorlib]System.Console::Read() conv.r8 stloc.0 ldstr "as a float: " call void [mscorlib]System.Console::WriteLine(string) ldloc.0 dup call void [mscorlib]System.Console::Write(float64) stloc.0 ldstr "Stored in location 0" call void [mscorlib]System.Console::WriteLine(string) ldloc.0 conv.i4 call void [mscorlib]System.Console::WriteLine(int32) call int32 [mscorlib]System.Console::Read() // to pause before closing window pop ret }
我只是在和CIL搞砸,但我觉得为了清晰起见,我会抛弃我的整个例子.它编译得很好但是当我输入5时它返回53作为浮点数和转换后的int32.
有人可以说明我做错了什么!
编辑:感谢Marc Gravell,我能够弄明白.对于那些感兴趣的人,这里是正确的代码:
.assembly variables {} .method public static void main() cil managed { .entrypoint .maxstack 8 .locals init (float64) ldstr "Enter a digit: " call void [mscorlib]System.Console::WriteLine(string) call string [mscorlib]System.Console::ReadLine() call int32 [mscorlib]system.int32::Parse(string) conv.r8 stloc.0 ldstr "as a float: " call void [mscorlib]System.Console::WriteLine(string) ldloc.0 dup call void [mscorlib]System.Console::Write(float64) stloc.0 ldstr "Stored in location 0" call void [mscorlib]System.Console::WriteLine(string) ldloc.0 conv.i4 call void [mscorlib]System.Console::WriteLine(int32) call int32 [mscorlib]System.Console::Read() // to pause before closing window pop ret }
解决方法
您也许可以使用Console.ReadLine和int.Parse.
Angular 8.3.2:将ArrayBuffer转换为Float32Array
以下代码有效:
this.wsSubscription = this.webSocket.createObservableSocket ("ws://localhost:8080")
.subscribe (
data =>
{
var bufferPromise = data.arrayBuffer();
data.arrayBuffer().then (buffer =>{
var arr32 = new Float32Array (buffer);
}