floating-point

What is the next normalised floating point number after(before) a normalised floating point number f?

Given a normalized floating point number f what is the next normalized floating point number after/before f. With bit twiddling, extracting mantissa and exponent I have: next_normalized(double&){ if mantissa is not all ones maximally denormalize while maintaining equality add 1 to mantissa normalize...

Best Language for a Mandelbrot Zoom?

Hi, I've been pretty interested in coding a Mandelbrot Set zoom and have already done it twice. However there were problems with each one. The first time I thought it'd be cool to do it in javascript... but that was so damn slow. Then I did it in C++, which worked great until you zoomed so far that the units on the graph got to the sma...

John Carmack's Unusual Fast Inverse Square Root (Quake III)

Hello, John Carmack has a special function in the Quake III source code which calculates the inverse square root of a float, 4x faster than regular (float)(1.0/sqrt(x)), including a strange 0x5f3759df constant. See the code below. Can someone explain line by line what exactly is going on here and why this works so much faster than the r...

How to calculate an arbitrary power/root?

I have a application which needs to raise a number to a fractional power. The target platform is an FPGA and I can get estimates on an FPU size for it, but I need an algorithm for raising a number to a fractional power just for a feasibility study. I'm assuming floating point as a worst case, I expect in practice we will be able to use...

Large numbers erroneously rounded in Javascript

See this code: <html> <head> <script src="http://www.json.org/json2.js" type="text/javascript"></script> <script type="text/javascript"> var jsonString = '{"id":714341252076979033,"type":"FUZZY"}'; var jsonParsed = JSON.parse(jsonString); console.log(jsonString, jsonParsed); </script> </head> <body> </body> </html> Wh...

Floating Point Subtraction in C results zero

I have a code wriiten in C which is intended for a 16-bit microcontroller. The code essentially does lot of floating point arithmatic. The arithmatic works fine till the result is positive; but in case of subtraction if the expected result is negative; I get a zero. result = 0.005 - 0.001; is correctly computed as 0.004 result = 0...

Trunc() function

Hi guys, look the follow code, why the result of Trunc function is different? procedure TForm1.Button1Click(Sender: TObject); var D: Double; E: Extended; I: Int64; begin D := Frac(101 / 100) * 100; E := Frac(101 / 100) * 100; I := Trunc(D); ShowMessage('Trunc(Double): ' + IntToStr(I)); // Trunc(Double): 1 I := Trunc(E)...

Can I rely on this to judge a square number in C++?

Can I rely on sqrt((float)a)*sqrt((float)a)==a or (int)sqrt((float)a)*(int)sqrt((float)a)==a to check whether a number is a perfect square? Why or why not? int a is the number to be judged. I'm using Visual Studio 2005. Edit: Thanks for all these rapid answers. I see that I can't rely on float type comparison. (If I wrote as a...

Why can I "fold" a range of integers into one half the size without losing information?

I'm trying to understand a paper on lossless compression of floating point numbers and get stuck on one particular step where the authors map a signed integer from a certain range into a range of half the size, losing information that I would think is required. I have a feeling that the authors are using some standard technique which is ...

How does a C# evaluate floating point in hover over and intermediate window versus compiled?

I am seeing something odd with storing doubles in a dictionary, and am confused as to why. Here's the code: Dictionary<string, double> a = new Dictionary<string, double>(); a.Add("a", 1e-3); if (1.0 < a["a"] * 1e3) Console.WriteLine("Wrong"); if (1.0 < 1e-3 * 1e3) ...

C++ Cout floating point problem

#include <iostream> using namespace std; int main() { float s; s = 10 / 3; cout << s << endl; cout.precision(4); cout << s << endl; return 0; } Why the output does not show 3.333 but only 3 ?? ...

C : erroneous output for "(long long int) = (long long int) * (double)"?

long long int A = 3289168178315264; long long int B = 1470960727228416; double D = sqrt(5); long long int out = A + B*D; printf("%lld",out); This gives result : -2147483648 I am not able to figure out why (it should be a positive result). Can somebody help? ...

Dealing with floating point numbers...

I was trying my hand at Google Code Jam and there was a question that used double values... I had a hard time coding in C, always somehow my answer would differ from the actual answer in some cases... What I wanted to know is that which language has the best floating point implementation and handling? This has happened a numerous time...

Formatting doubles for output in C#

Running a quick experiment related to Is double Multiplication Broken in .NET? and reading a couple of articles on C# string formatting, I thought that this: { double i = 10 * 0.69; Console.WriteLine(i); Console.WriteLine(String.Format(" {0:F20}", i)); Console.WriteLine(String.Format("+ {0:F20}", 6.9 - i)); Console....

Converting float to double

How expensive is the conversion of a float to a double? Is it as trivial as an int to long conversion? EDIT: I'm assuming a platform where where float is 4 bytes and double is 8 bytes ...

Diff tool that ignores floating-point formats (but not values) in text?

I'm looking for a diff tool that can also compare floating point values (within some tolerance) in text files. This is in addition to typical text-comparison diff functions, with options to ignore whitespace, ignore case, etc. A GUI (or full-screen console UI) is okay, but I'd really prefer a stream-oriented (stdin/stdout) tool. Here's ...

How to handle precision in Floating point arithmetic in C

In programming contests, floating point arithmetic related questions say "the error is answer must be less than 1e-6" or "The answer must be correct upto 6 decimal places". Does this mean that I can perform calculations on FP variables without worrying about the precision and only while printing I should write like printf("%.6lf",a); ...

How do I convert double to string using only math.h?

I am trying to convert a double to a string in a native NT application, i.e. an application that only depends on ntdll.dll. Unfortunately, ntdll's version of vsnprintf does not support %f et al., forcing me to implement the conversion on my own. The aforementioned ntdll.dll exports only a few of the math.h functions (floor, ceil, log, p...

How can I declare a class variable as floating point in Moose?

How can I declare a class variable as floating point in Moose? My (fictional) sample below produces errors for "Real", "Number" etc ... "Str" works but defeats the purpose .. searching/Google doesn't help, since I can't hit the correct search terms... PROBLEM.pm package PROBLEM; use strict; use warnings; use Moose; has 'PROBLEM'=> ...

Fast exponentiation: real^real (C++ MinGW, Code::Blocks)

I am writing an application where in a certain block I need to exponentiate reals around 3*500*500 times. When I use the exp(y*log(x)) algorithm, the program noticeably lags. It is significantly faster if I use another algorithm based on playing with data types, but that algorithm isn't very precise, although provides decent results for ...