floating-point-conversion

Half-precision floating-point arithmetic on Intel chips

假装没事ソ 提交于 2019-12-21 04:57:32
问题 Is it possible to perform half-precision floating-point arithmetic on Intel chips? I know how to load/store/convert half-precision floating-point numbers [1] but I do not know how to add/multiply them without converting to single-precision floating-point numbers. [1] https://software.intel.com/en-us/articles/performance-benefits-of-half-precision-floats 回答1: Is it possible to perform half-precision floating-point arithmetic on Intel chips? Yes, apparently the on-chip GPU in Skylake and later

Converting float to char*

北城余情 提交于 2019-12-20 12:04:02
问题 How can I convert a float value to char* in C language? 回答1: char buffer[64]; int ret = snprintf(buffer, sizeof buffer, "%f", myFloat); if (ret < 0) { return EXIT_FAILURE; } if (ret >= sizeof buffer) { /* Result was truncated - resize the buffer and retry. } That will store the string representation of myFloat in myCharPointer . Make sure that the string is large enough to hold it, though. snprintf is a better option than sprintf as it guarantees it will never write past the size of the

Float24 (24 bit floating point) to Hex?

喜夏-厌秋 提交于 2019-12-20 05:27:47
问题 I'm using float 24 bit to store a floating point value in a compiler MRK III from NXP. It stores the 24 bit float value as 3 byte Hex in Data memory. Now when I'm using IEEE 754 float point conversion to retrieve the number back from binary to real, I'm getting something very strange. Let me put it this way with an example - Note - "since my compiler supports float 24 bit (along with float 32), I'm assigning value something like this." Sample Program : float24 f24test; float f32test; f32test=

Reading floats into an array

家住魔仙堡 提交于 2019-12-20 04:22:38
问题 How could I read let's say 10 floats and store them in an array without wasting any memory? 回答1: int size = 10; float vet[size]; for(i = 0; i < size; i++){ scanf("%f", &vet[i]); } As simple as it could be :) 回答2: Aha. It's not reading the floats that's the problem, it's the memory. You read in i , and you need an array that holds exactly i floats. This really does smell like homework, which is fine, but I'm too much the teacher to give you the full answer. So I'll tell you, what you need is a

Convert signed IEEE 754 float to hexadecimal representation

眉间皱痕 提交于 2019-12-19 18:19:07
问题 I'm using a front-end of Lua which is unfortunately outdated, so I'm stuck with version 5.1 here, meaning the bit32 library is out of reach (which I probably could have used to convert this). So I'm wondering if anyone knows of a way I could implement either a floating-point to binary (digits) function or, better yet, floating-point to hex. The best I've been able to come up with so far is a decimal to binary/hex function... 回答1: The following functions uses some code from François Perrad's

Convert signed IEEE 754 float to hexadecimal representation

▼魔方 西西 提交于 2019-12-19 18:18:08
问题 I'm using a front-end of Lua which is unfortunately outdated, so I'm stuck with version 5.1 here, meaning the bit32 library is out of reach (which I probably could have used to convert this). So I'm wondering if anyone knows of a way I could implement either a floating-point to binary (digits) function or, better yet, floating-point to hex. The best I've been able to come up with so far is a decimal to binary/hex function... 回答1: The following functions uses some code from François Perrad's

Multiplying float values “possible lossy conversion from double to float”

孤街醉人 提交于 2019-12-17 21:17:29
问题 I have this problem wherein I have to convert kilometers into miles. I'm a novice programmer so bear with me. Here's my code so far: import java.util.Scanner; public class problem1 { public static void main (String args[]) { float m; float km; Scanner input=new Scanner(System.in); System.out.print("Please enter a distance in kilometers:"); km=input.nextFloat(); m=km*0.621371; System.out.println("This is equal to: "+m); } } It gives me an error saying: Incompatible types:possible lossy

Comparing float and double

☆樱花仙子☆ 提交于 2019-12-17 09:58:16
问题 #include <stdio.h> int main(void){ float a = 1.1; double b = 1.1; if(a == b){ printf("if block"); } else{ printf("else block"); } return 0; } Prints: else block #include <stdio.h> int main(void){ float a = 1.5; double b = 1.5; if(a == b){ printf("if block"); } else{ printf("else block"); } return 0; } Prints: if block What is the logic behind this? Compiler used: gcc-4.3.4 回答1: This is because 1.1 is not exactly representable in binary floating-point. But 1.5 is. As a result, the float and

Comparing float and double

亡梦爱人 提交于 2019-12-17 09:58:10
问题 #include <stdio.h> int main(void){ float a = 1.1; double b = 1.1; if(a == b){ printf("if block"); } else{ printf("else block"); } return 0; } Prints: else block #include <stdio.h> int main(void){ float a = 1.5; double b = 1.5; if(a == b){ printf("if block"); } else{ printf("else block"); } return 0; } Prints: if block What is the logic behind this? Compiler used: gcc-4.3.4 回答1: This is because 1.1 is not exactly representable in binary floating-point. But 1.5 is. As a result, the float and

C - Serialization of the floating point numbers (floats, doubles)

天涯浪子 提交于 2019-12-17 06:45:24
问题 How to convert a floating point number into a sequence of bytes so that it can be persisted in a file? Such algorithm must be fast and highly portable. It must allow also the opposite operation, deserialization. It would be nice if only very tiny excess of bits per value (persistent space) is required. 回答1: Assuming you're using mainstream compilers, floating point values in C and C++ obey the IEEE standard and when written in binary form to a file can be recovered in any other platform,