floating-point-conversion

Swift: extract float from byte data

拜拜、爱过 提交于 2019-11-30 10:05:34
I'm looking for a robust and elegant way to extract four big-endian bytes from an array as a Float. I can get a UInt32 with the bits via something like this: let data: [Byte] = [0x00, 0x00, 0x00, 0x40, 0x86, 0x66, 0x66, 0x00] let dataPtr = UnsafePointer<Byte>(data) let byteOffset = 3 let bits = UnsafePointer<UInt32>(dataPtr + byteOffset)[0].bigEndian But I can't figure out a good way to convert this into a Float in Swift. For example, in Java: float f = Float.intBitsToFloat(bits) or in C: float f = *(float *)&bits; I tried casting the dataPtr to a float UnsafePointer, but then the endianness

Working With Floats and Integers

空扰寡人 提交于 2019-11-30 09:25:21
问题 I've created an ATM like program which holds the money in a person's account. When the person takes out a withdrawal, it subtracts the withdrawal from the account along with a .50 surcharge. The problem I'm having is working with both integers and floats in this program. I converted the integer account to a floating point number but I get an error message when I try to print out the statement. Can someone tell me what I'm doing wrong? #include <stdio.h> int main (void) { int account = 2000;

Handling a quadruple precision floating point (128-bit) number in java

旧城冷巷雨未停 提交于 2019-11-30 03:34:58
问题 I need to make use of numbers coming from another system that are 128-bit (quadruple-precision) floating point numbers in java. Considering that there is no equivalent type in java, I would like to reduce the precision of the numbers using java code so they can be stored in a java double. This can be done fairly easily in c or using assembly but I would like to do it purely in java. It is fair to assume that the quadruple-precision number is stored in a 128-bit byte array in java. Is there a

How to check if a user input is a float

放肆的年华 提交于 2019-11-29 15:43:22
I'm doing Learn Python the Hard Way exercise 35. Below is the original code, and we're asked to change it so it can accept numbers that don't have just 0 and 1 in them. def gold_room(): print "This room is full of gold. How much do you take?" next = raw_input("> ") if "0" in next or "1" in next: how_much = int(next) else: dead("Man, learn to type a number.") if how_much < 50: print "Nice, you're not greedy, you win!" exit(0) else: dead("You greedy bastard!") This is my solution, which runs fine and recognizes float values: def gold_room(): print "This room is full of gold. What percent of it

Swift: extract float from byte data

孤者浪人 提交于 2019-11-29 15:05:06
问题 I'm looking for a robust and elegant way to extract four big-endian bytes from an array as a Float. I can get a UInt32 with the bits via something like this: let data: [Byte] = [0x00, 0x00, 0x00, 0x40, 0x86, 0x66, 0x66, 0x00] let dataPtr = UnsafePointer<Byte>(data) let byteOffset = 3 let bits = UnsafePointer<UInt32>(dataPtr + byteOffset)[0].bigEndian But I can't figure out a good way to convert this into a Float in Swift. For example, in Java: float f = Float.intBitsToFloat(bits) or in C:

MySQL “greater than” condition sometimes returns row with equal value

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 13:52:55
I'm running into a baffling issue with a basic MySQL query. This is my table: id | rating 1 | 1317.17 2 | 1280.59 3 | 995.12 4 | 973.88 Now, I'm attempting to find all rows where the rating column is larger than a certain value. If I try the following query: SELECT * FROM (`users`) WHERE `rating` > '995.12' It correctly returns 2 . But, if I try SELECT * FROM (`users`) WHERE `rating` > '973.88' it returns 4 ! So it's as if it thinks the 973.88 in the table is greater than 973.88, but it doesn't make the same mistake with 995.12. This happens regardless of whether I run the query from a PHP

Random float number

和自甴很熟 提交于 2019-11-29 03:54:30
I wrote this function to get a pseudo random float between 0 .. 1 inclusive: float randomFloat() { float r = (float)rand()/(float)RAND_MAX; return r; } However, it is always returning 0.563585. The same number no matter how many times I run my console application. EDIT: Here is my entire application if needed: #include <stdio.h> #include <stdlib.h> float randomFloat() { float r = (float)rand() / (float)RAND_MAX; return r; } int main(int argc, char *argv[]) { float x[] = { 0.72, 0.91, 0.46, 0.03, 0.12, 0.96, 0.79, 0.46, 0.66, 0.72, 0.35, -0.16, -0.04, -0.11, 0.31, 0.00, -0.43, 0.57, -0.47, -0

Best way of checking if a floating point is an integer

被刻印的时光 ゝ 提交于 2019-11-28 22:33:30
问题 [There are a few questions on this but none of the answers are particularly definitive and several are out of date with the current C++ standard]. My research shows these are the principal methods used to check if a floating point value can be converted to an integral type T . if (f >= std::numeric_limits<T>::min() && f <= std::numeric_limits<T>::max() && f == (T)f)) using std::fmod to extract the remainder and test equality to 0. using std::remainder and test equality to 0. The first test

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

♀尐吖头ヾ 提交于 2019-11-28 14:27:45
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 conversion from double to float. You are trying to set a double to a float variable To fix, change this line m

Confusion with floating point numbers

南楼画角 提交于 2019-11-28 13:58:15
int main() { float x=3.4e2; printf("%f",x); return 0; } Output: 340.000000 // It's ok. But if write x=3.1234e2 the output is 312.339996 and if x=3.12345678e2 the output is 312.345673 . Why are the outputs like these? I think if I write x=3.1234e2 the output should be 312.340000 , but the actual output is 312.339996 using GCC compiler. Not all fractional numbers have an exact binary equivalent so it is rounded to the nearest value. Simplified example, if you have 3 bits for the fraction, you can have: 0 0.125 0.25 0.375 ... 0.5 has an exact representation, but 0.1 will be shown as 0.125. Of