Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [floating-point]

The tag has no usage guidance.

15
votes
6answers
3k views

How to implement float hashing with approximate equality

Let's say we have the following Python class (the problem exists in Java just the same with equals and hashCode) class Temperature: def __init__(self, degrees): self.degrees = degrees ...
9
votes
3answers
440 views

Do i still need to use fixed point to guarantee computers get the same result for math operations anymore?

I was told most modern computers follow the same floating point standard, does this mean they will all get the same float answer for a given math operation if the inputs are the same? I ask because i ...
1
vote
1answer
132 views

How can I used fixed-point arithmetic to convert from Celsius to Fahrenheit?

Disclaimer: I realize this is not the easiest or even best way to handle this and I am asking largely out of a sense of curiosity, not because I'm convinced this is the proper way. It's a fun puzzle I'...
0
votes
1answer
169 views

what *should* you get when you add minus-zero to plus-zero? [closed]

this Wikipedia article did not address this contingency and i am having trouble finding a definitive IEEE-754 document that addresses this.
30
votes
8answers
14k views

Does comparing equality of float numbers mislead junior developers even if no rounding error occurs in my case?

For example, I want to show a list of buttons from 0,0.5,... 5, which jumps for each 0.5. I use a for loop to do that, and have different color at button STANDARD_LINE: var MAX=5.0; var DIFF=0.5 var ...
1
vote
4answers
436 views

Unit test a generic floating point equality function

I've written a function that tests two floating point numbers for approximate equality (see this Code Review question). I'd like to unit test my function, but I'm not positive of the best way to do ...
4
votes
0answers
180 views

Who first said, “If you think floating point is the answer, you don't understand the problem.” [closed]

I had a C (programming language) teacher at the Lowell Institute at MIT who said this in the Spring of 1996. It's an overstatement, but still seems like a classic quote to me. I've had no luck ...
4
votes
0answers
72 views

Rounding floats after modf()

I am trying to make an effective modf() function that uses floats instead of doubles. An embedded application I am working on only supports the float type. My modf() function works as expected with ...
1
vote
3answers
399 views

Why can less precise data like float be faster than larger, more precise data like double?

I am currently reading a chapter in a textbook on Processor Architecture and saw the following statement: The less precision there is, the less space is occupied by a program variable in memory. ...
1
vote
3answers
1k views

Choosing an epsilon when comparing floating point numbers across different systems

I am transcribing thousands of lines of computational code from MATLAB to C++. I don't fully understand the math myself, but I can run it with MATLAB, assume it's correct, and compare the results to ...
84
votes
6answers
17k views

Why are floats still part of the Java language when doubles are mostly recommended instead?

In every place I've looked, it says that double is superior to float in almost every way. float has been made obsolete by double in Java, so why is it still used? I program a lot with Libgdx, and ...
0
votes
2answers
296 views

Floating point accuracy and conversions

I am trying to build a class where user can compute floating operations with a particular accuracy. For example, if the user wants to have up to 4 digit accuracy, everything less than 0.0001 is zero ...
5
votes
2answers
1k views

Floating point calculation being too exact

In Java, I've run this method: private static void floatAddition() { float a = 0.1F, b = 0.2F, c = a+b; System.out.println(a + " + " + b + " = " + c); } and the result is 0.3 for ...
-1
votes
1answer
226 views

Floating point absorption phenomena and ULP

Numerical analysis text books talks about the absorption phenomena (Introduction to Numerical Analysis and Scientific Computing; Nabil Nassif; Dolly Khoueiri Fayyad; CRC Press; 2014) when adding ...
5
votes
4answers
9k views

Avoiding Division by Zero Using Float Comparison

Source code analyzers (like SonarQube) complain about float (or double) equality comparisons because equality is a tricky thing with floats; the values compared can be the results of computations ...
9
votes
2answers
390 views

Should `Vector<float>.Equals` be reflexive or should it follow IEEE 754 semantics?

When comparing floating point values for equality, there are two different approaches: NaN not being equal to itself, which matches the IEEE 754 specification. NaN being equal to itself, which ...
-2
votes
1answer
723 views

How to convert byte-array (4 bytes) back into float? [closed]

A piece of code I'm trying to debug generated a sequence of 4 bytes from a known float number. I'm trying to verify the float number given the four bytes. The (known) float 8.03 generated the bytes ...
4
votes
0answers
471 views

Rationale of IEEE754 direct rounding behavior near infinity [closed]

A preface: it's well known that IEEE754 defines five rounding modes (in 2008 edition terms, with my abbreviations): rounding to nearest, ties to even (RNE) - the default mode for binary arithmetic; ...
4
votes
3answers
1k views

Is order of arguments in an arithmetic expression important to achieve as most exact result as possible (speed is not necessary)?

Actually, in this question I do not ask about particular language or architecture, however I understand there might be some differences. In physics / engineering it is usually better to handle ...
4
votes
2answers
22k views

How to calculate min/max values of floating point numbers?

I'm trying to calculate the min/max, or the lowest to highest value range of a 48 bit floating point type MIL-STD-1750A (PDF) (WIKI). Ex: How a double range is 1.7E +/- 308 I've looked around for ...
7
votes
2answers
3k views

Why does Python's math.ceil return a float?

While I understand that the difference between integral and long values is blurred in Python, the difference between floats and integral values is not. Therefore, I'm having a difficult time ...
0
votes
1answer
278 views

floating-point number stored in float variable has other value [duplicate]

I'm reading a book about C programming By "King.K.N" and I'm reading the following statement: If we store 0.1 in a float variable, we may later find that the variable has a value such as 0....
9
votes
1answer
475 views

How can unums emulate IEEE's negative zero?

I am currently reading "The End of Error - Unum Computing" by John Gustafson (Youtube). What I am still not sure about is how the cases handled in IEEE by negatively signed zero are handled with unums....
2
votes
3answers
1k views

How can a computer round the last digit in a floating point representation?

I'm confused by how a computer rounds off the last digit in the floating point representation. For example, I'm told that x=1.24327789 is stored in a computer with a 6-digit capacity then it;s ...
1
vote
2answers
135 views

How to implement rounding in an all-purpose stack language using different types?

Disclaimer: If you are not terribly interested in numerics and mathematical processes, this is most likely nothing for you. I am currently a bit stuck in a development process of a private project I ...
62
votes
5answers
12k views

Why is negative zero important?

I'm confused about why we care about different representations for positive and negative zero. I vaguely recall reading claims that having a negative zero representation is extremely important in ...
3
votes
1answer
303 views

Is the behaviour of floating-point arithmetic defined by the c++ standard?

I started writing this as a SO question, but I think it's better suited here. While I'm happy for a simple answer I'm also fond of the "Teach a Man to Fish" philosophy so I'm happy for people to point ...
1
vote
1answer
2k views

adding the digits of a number

I am trying to write a program that asks the user for a decimal number and then calculates the sum of its digits. for example if the number is 123.25 then the sum will be 1+2+3+2+5=13. I decided to ...
-1
votes
1answer
138 views

Is this statement correct for floating point numbers?

The decimal point can "float" to accommodate larger numbers while staying in 32-bits which is why float is considered "in-accurate" Is this an accurate statement? I want to know if I understand ...
32
votes
9answers
10k views

Why are floating point numbers used often in Science/Engineering?

While investigating the accuracy of floating point numbers, I've seen in a few places a statement similar to "float and double are (designed for/used often in) engineering and scientific calculation"...
2
votes
1answer
4k views

How is a floating point number represented in Java?

Which algorithm does java uses to convert floating point number (Ex:0.15625) into binary(0.00101)? Whether java uses normalized form or denormalized format? If I type float ab=0.15625 in Java source ...
10
votes
1answer
332 views

Implied precision of floating-point functions

While reviewing another programmer's implementation of a function to calculate the normal distribution CDF, I made a suggestion to either replace the entire implementation with Python's built-in ...
2
votes
2answers
225 views

How are some IEEE 754 floats printed succinctly?

Using this calculator, I can see how the decimal ".8" and the expression ".7 + .1" have different representations: .8 = 1.1001100110011001100110011001100110011001100110011010 *2-1 .7 + .1 = 1....
2
votes
0answers
46 views

Time passage arithmetic explanation

I ported this from http://www.effectgames.com/effect/article.psp.html/joe/Old_School_Color_Cycling_with_HTML5 some time ago. However i'm now wanting to modify it for the purpose of changing it from ...
41
votes
3answers
6k views

Are there numbers that are not representable in base 10 but can be represented in base 2?

C# has the decimal type which is used for numbers that needs exact representation in base 10. For instance, 0.1 cannot be represented in base 2 (e.g. float and double) and will always be an ...
10
votes
3answers
27k views

Why is BigDecimal the best data type for currency? [duplicate]

I was reading this question and the accepted answer says that BigDecimal is the best type for representing currency values. I've also seen several other questions support the use of big decimal. Why ...
5
votes
1answer
1k views

Custom class for monetary amount (money value) instead of BigDecimal

On StackOverflow I found multiple advices to stick with BigDecimal in Java instead of reinventing the wheel. Despite that, I decided to create my custom class, and I would ask if that was a good idea ...
6
votes
3answers
4k views

When do rounding problems become a real problem? Is the least significant digit being one off really a big deal? [duplicate]

If I do .1 + .1 + .1 in Python, I get 0.30000000000000004. (I am not asking about Python in particular, and do not want Python specific answers.) The only problem I can see with this is 0....
2
votes
1answer
407 views

Will we go back(?) to fixed-point arithmetic in the near future? [closed]

As far as I recall, this has been the ongoing trend of the past, I'm just a student, so I might be wrong: Long ago: Integer numbers and calculations. Very precise, however cannot present a big range. ...
29
votes
11answers
11k views

Why do we still use floats? [duplicate]

I understand why floats served a purpose in the past. And I think I can see why they're useful in some simulation examples today. But I think those example are more exceptional than common. So I ...
3
votes
2answers
572 views

Why does IEEE 754 Single Float have 8 bits for exponent but only allow 38 base ten Max Value

Why is it that the IEEE 754 standard provide 8 bits for the exponent but puts a range maximum of 38 on the largest acceptable exponent? Seems kind of ridiculous to have the data storage there but not ...
6
votes
3answers
878 views

Unit (regression) testing scientific algorithms given floating point behavior

I have been working on a project and running into a very difficult problem. The problem can be stated simply as how to unit-test numerical algorithms. However if you just took this simple statement ...
16
votes
2answers
41k views

Can anyone explain representation of float in memory?

This is not duplicate question as I read the previous question. Can anyone help me in understanding how float values are stored in the memory . My doubt is here float values contain '.' (for example ...
0
votes
1answer
133 views

How can I handle clock hands using doubles that suffers at most O(log n) corrupt bits at nth frame of display?

I presently have a clock app that calculates from scratch at every iteration. This means O(1) corrupt bits in my doubles and heavy object creation and deletion as well. I am wary of running ...
2
votes
4answers
393 views

Generic term for “objects” vs “fundamental types”?

What are the exact terms to call data types with a logic structure (like C structures, C++ or Java objects) versus fundamental data types (like numeric types, characters, booleans...) independently of ...
2
votes
3answers
533 views

Why is float the default in the majority of languages?

In programming languages, for represent numbers you have (mainly) two types: Int(s) and floats. The us of Int is very easy. However floats cause a lot of unexpected surprises: http://anh.cs.luc.edu/...
11
votes
2answers
10k views

How Lua handles both integer and float numbers?

As far as I remember myself programming I was taught not to compare floating point numbers for equality. Now, while reading Programming in Lua about Lua number type, I found following: The number ...
2
votes
1answer
6k views

Addition of double's is NOT Equal to Sum of the double as a whole

I am aware of the floating point errors as I had gained some knowledge with my question asked here in SE Floating Point Errors. What I am finding it difficult to understand is, the output of the ...
16
votes
5answers
15k views

Solutions for floating point rounding errors

In building an application that deals with a lot of mathematical calculations, I have encountered the problem that certain numbers cause rounding errors. While I understand that floating point is ...
1
vote
2answers
1k views

Implementing base-10 floating point division

I'm implementing floating-point arithmetic, for a micro-controller which does not support floating point numbers, in either hardware or software. (Software being "written" in a sort of electrical ...