floating-point

Using write to print floating point numbers.

Hi, The write function does not print a floating point number in the following code: #include <unistd.h> int main(){ float f = 4.5; write(1,&f,sizeof float); return 0; } This results in: �@ Whereas: int main(){ char *c = "Hello world"; write (1,c,strlen(c)+1); return 0; } Prints Hello world as exp...

How do calculators work with precision?

Hello! I wonder how calculators work with precision. For example the value of sin(M_PI) is not exactly zero when computed in double precision: #include <math.h> #include <stdio.h> int main() { double x = sin(M_PI); printf("%.20f\n", x); // 0.00000000000000012246 return 0; } Now I would certainly want to print zero when us...

How to make gcc on SUN calculate floating points the same way as in Linux

I have a project where I have to perform some mathematics calculations with double variables. The problem is that I get different results on SUN Solaris 9 and Linux. There are a lot of ways (explained here and other forums) how to make Linux work as Sun, but not the other way around. I cannot touch the Linux code, so it is only SUN I can...

MySQL float values jumping around on insert?

So i have a SQL table setup as such CREATE TABLE IF NOT EXISTS `points` ( `id` int(11) NOT NULL auto_increment, `lat` float(10,6) NOT NULL, `lng` float(10,6) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM; And im inserting stuff like INSERT INTO `points` (`lat`, `lng`) VALUES ('89.123456','-12.123456'); Gives me a row with l...

Does "epsilon" really guarantees anything in floating-point computations?!

To make the problem short let's say I want to compute expression: a / (b - c) on float's. To make sure the result is meaningful, I can check if 'b' and 'c' are inequal: float EPS = std::numeric_limits<float>::epsilon(); if ((b - c) > EPS || (c - b) > EPS) { return a / (b - c); } but my tests show it is not enough to guarantee eit...

Real life example fo Floating Point error

Is there any examples of a company that was burned by floating point data that caused a rounding issue? We're implementing a new system and all the monetary values are stored in floats. I think if i can show actual examples of why this has failed it'll have more weight than the theory of why the values can't be stored properly. ...

AudioQueue recording as float

Hi guys, I would like to have the result from my recording as a float in the range [0.0, 1.0], alternatively [-1.0, 1.0] because of a bit of math I want to do on it. When I set my recordingformat to be in float, like this: mRecordFormat.mFormatFlags = kLinearPCMFormatFlagIsFloat; I get: Error: AudioQueueNewInput failed ('fmt?') D...

Float to binary in C++

Hi, I'm wondering if there is a way to represent a float using a char in C++? For example: int main() { float test = 4.7567; char result = charRepresentation(test); return 0; } I read that probably using bitset I can do it but I'm not pretty sure. Let's suppose that my float variable is 01001010 01001010 010...

fixed floating point calculations in Java

In Java I need some hint to declare floating point variable that in all virtual machine run uniquely and show number unique float number in all machine ( mobile machine and PC ) ...

Interview Q: find angle between hour and minute hands in an analog clock

I was given this interview question recently: Given a 12-hour analog clock, compute in degree the smaller angle between the hour and minute hands. Be as precise as you can. I'm wondering what's the simplest, most readable, most precise algorithm is. Solution in any language is welcome (but do explain it a bit if you think it's nece...

Delphi Math: Why is 0.7<0.70?

If I have variables a, b, an c of type double, let c:=a/b, and give a and b values of 7 and 10, then c's value of 0.7 registers as being LESS THAN 0.70. On the other hand, if the variables are all type extended, then c's value of 0.7 does not register as being less than 0.70. This seems strange. What information am I missing? ...

How do you handle floating point rounding problems?

Seeing this post about floating point errors on slashdot, I got curious what other kind of solutions exist to deal with such kind of floating point rounding errors. So what was the floating point bug you learned most from, and what did you learn from it? EDIT: I am working within a project where we have to deal a lot with floating poin...

C - floating point rounding

I'm trying to understand how floating point numbers work. I think I'd like to test out what I know / need to learn by evaluating the following: I would like to find the smallest x such that x + 1 = x, where x is a floating point number. As I understand it, this would happen in the case where x is large enough so that x + 1 is closer t...

Anything wrong with this function for comparing floats?

When my Floating-Point Guide was yesterday published on slashdot, I got a lot of flak for my suggested comparison function, which was indeed inadequate. So I finally did the sensible thing and wrote a test suite to see whether I could get them all to pass. Here is my result so far. And I wonder if this is really as good as one can get wi...

Doing arithmetic with up to two decimal places in Python?

I have two floats in Python that I'd like to subtract, i.e. v1 = float(value1) v2 = float(value2) diff = v1 - v2 I want "diff" to be computed up to two decimal places, that is compute it using %.2f of v1 and %.2f of v2. How can I do this? I know how to print v1 and v2 up to two decimals, but not how to do arithmetic like that. The ...

floating point precision in ruby on rails model validations

Hello I am trying to validate a dollar amount using a regex: ^[0-9]+\.[0-9]{2}$ This works fine, but whenever a user submits the form and the dollar amount ends in 0(zero), ruby(or rails?) chops the 0 off. So 500.00 turns into 500.0 thus failing the regex validation. Is there any way to make ruby/rails keep the format entered by the us...

Numeric comparison difficulty in R

I'm trying to compare two numbers in R as a part of a if-statement condition: (a-b) >= 0.5 In this particular instance, a = 0.58 and b = 0.08... and yet (a-b) >= 0.5 is false. I'm aware of the dangers of using == for exact number comparisons, and this seems related: (a - b) == 0.5) is false, while all.equal((a - b), 0.5) is true. ...

How does GDI+ support floating point values?

Given that GDI+ is a wrapper around GDI, how does it handle floating point values? I don't see any support for floating point co-ordinates in the GDI documentation. ...

Map a 32 bit float to a 32 bit integer

Is there a way to map floats to ints or unsigned ints so that with the exception of NaN, order is preserved? So if a and b are floats, and F is the mapping function, a < b implies F(a) < F(b) and a == b implies F(a) == F(b) ...

Loss of precision - int -> float or double

I have an exam question i am revising for and the question is for 4 marks "In java we can assign a int to a double or a float". Will this ever loose infromation and why? I have put that because ints are normally of fixed length or size - the precision for sotring data is finite, where storing information in floating point can be infin...