I am trying to create an array of values. These values should be "2.4,1.6,.8,0". I am subtracting .8 at every step.

This is how I am doing it (code snippet):

```
float mean = [[_scalesDictionary objectForKey:@"M1"] floatValue]; //3.2f
float sD = [[_scalesDictionary objectForKey:@"SD1"] floatValue]; //0.8f
nextRegion = mean;
hitWall = NO;
NSMutableArray *minusRegion = [NSMutableArray array];
while (!hitWall) {
nextRegion -= sD;
if(nextRegion<0.0f){
nextRegion = 0.0f;
hitWall = YES;
}
[minusRegion addObject:[NSNumber numberWithFloat:nextRegion]];
}
```

I am getting this output:

```
minusRegion = (
"2.4",
"1.6",
"0.8000001",
"1.192093e-07",
0
)
```

I do not want the incredibly small number between .8 and 0. Is there a standard way to truncate these values?

Another way to do this is to multiply the numbers you get by subtraction by 10, then convert to an integer, then divide that integer by by 10.0.

You can do this easily with the floor function (floorf) like this:

float newValue = floorf(oldVlaue*10)/10;

You're looking at good old floating-point rounding error. Fortunately, in your case it should be simple to deal with. Just clamp:

```
if( val < increment ){
val = 0.0;
}
```

Although, as Eric Postpischil explained below:

Clamping in this way is a bad idea, because sometimes rounding will cause the iteration variable to be slightly less than the increment instead of slightly more, and this clamping will effectively skip an iteration. For example, if the initial value were 3.6f (instead of 3.2f), and the step were .9f (instead of .8f), then the values in each iteration would be slightly below 3.6, 2.7, 1.8, and .9. At that point, clamping converts the value slightly below .9 to zero, and an iteration is skipped.

Therefore it might be necessary to subtract a small amount when doing the comparison.

A better option which you should consider is doing your calculations with integers rather than floats, then converting later.

```
int increment = 8;
int val = 32;
while( val > 0 ){
val -= increment;
float new_float_val = val / 10.0;
};
```

Neither 3.2 nor .8 is exactly representable as a 32-bit float. The representable number closest to 3.2 is 3.2000000476837158203125 (in hexadecimal floating-point, 0x1.99999ap+1). The representable number closest to .8 is 0.800000011920928955078125 (0x1.99999ap-1).

When 0.800000011920928955078125 is subtracted from 3.2000000476837158203125, the exact mathematical result is 2.400000035762786865234375 (0x1.3333338p+1). This result is also not exactly representable as a 32-bit float. (You can see this easily in the hexadecimal floating-point. A 32-bit float has a 24-bit significand. “1.3333338” has one bit in the “1”, 24 bits in the middle six digits, and another bit in the ”8”.) So the result is rounded to the nearest 32-bit float, which is 2.400000095367431640625 (0x1.333334p+1).

Subtracting 0.800000011920928955078125 from that yields 1.6000001430511474609375 (0x1.99999cp+0), which is exactly representable. (The “1” is one bit, the five nines are 20 bits, and the “c” has two significant bits. The low bits two bits in the “c” are trailing zeroes and may be neglected. So there are 23 significant bits.)

Subtracting 0.800000011920928955078125 from that yields 0.800000131130218505859375 (0x1.99999ep-1), which is also exactly representable.

Finally, subtracting 0.800000011920928955078125 from that yields 1.1920928955078125e-07 (0x1p-23).

The lesson to be learned here is the floating-point does not represent all numbers, and it rounds results to give you the closest numbers it can represent. When writing software to use floating-point arithmetic, you must understand and allow for these rounding operations. One way to allow for this is to use numbers that you know can be represented. Others have suggested using integer arithmetic. Another option is to use mostly values that you know can be represented exactly in floating-point, which includes integers up to 2^{24}. So you could start with 32 and subtract 8, yielding 24, then 16, then 8, then 0. Those would be the intermediate values you use for loop control and continuing calculations with no error. When you are ready to deliver results, then you could divide by 10, producing numbers near 3.2, 2.4, 1.6, .8, and 0 (exactly). This way, your arithmetic would introduce only one rounding error into each result, instead of accumulating rounding errors from iteration to iteration.

Similar Questions

I have read several questions about JTable sorting: Problems with JTable sorting of integer values JTable + Sorting specific field but I still can't manage to get it right, for some reason. I have wri

I'd like to store two float values in a single 32 bit float variable. The encoding will happen in C# while the decoding is to be done in a HLSL shader. The best solution I've found so far is hard-wiri

My text file is having the following values,i want to get only the second float number after the first float number and space so for the first row 0,1 0.7302967433402214 i want the number 0.73029674

I'd like to divide two Int values in Haskell and obtain the result as a Float. I tried doing it like this: foo :: Int -> Int -> Float foo a b = fromRational $ a % b but GHC (version 6.12.1) tel

I have a situation where I need to compute the mean of all the values in a particular column. I got this SQL statement which works: SELECT SUM(`unitCost`)/COUNT(*) FROM `items` WHERE `itemID_FK`=%d I

I am trying to render a float value using NSNumberFormatter's SpellOutStyle, but the results are unsatisfactory, and I haven't been able to find a solution so far. The issue is, when given a float val

I have problem with sql query statement. How to pass argument values to sql query properly? When I click button it shows INVALID ALTER COMMAND. Old_table_name, New_table_name are text fields public

How can i generate float random values in C? (also negative)

Submit buttons don't seem to clear properly without also adding float. Can someone clear this up for me (no pun intended). http://jsfiddle.net/qb5TH/ CSS: .left {float:left} .clear {clear:both} Form:

vbscript math function : FormatNumber FormatNumber(4.33 - 4.28, 1, -2, -2, -2) //answer 0.0 FormatNumber(0.05, 1, -2, -2, -2) //answer 0.1 even FormatNumber(cdbl(4.33) - cdbl(4.28), 1, -2, -2, -2) //

I want to take the value from my slider and without rounding up, crop the decimals. So if I have a float value of 10.973. I want it in an integer as 10. I dont use negative values but if the slider va

I have Defined the Scroller value to be hours starting 15 min interval and the wheel should have has values like 0.25 0.5 0.75 1 1.25...23.75 but its sorting the values like tits showing the integers

I want to build a regular expression for float number with maximum Length 5. My number can have separator ',' or '.' or it can be a natural number (not float) I Want to allow any of this values XXXXX

I have the double value like 12.256852651 and I want to display it as 12.257 as a float number without converting it in to a string type. How can I do it in C# ?

Table1 Id value 001 2.3 002 1.3 003 3 004 5.3 ... ... value column datatype is float Note: value column will be .3 always, it will not come like .1, .2, .4, .5 to .9 Now i want to display .5 instead

I'm about to mark maybe 100,000 records retroactively/posthoc-wise with category-indicating string or integer values. There are more to come. The categories to be marked by this column reflect a scala

I am trying to truncate some float values contained in the same string but I'm not quite sure how to proceed. Currently I am using the Python xml.dom.minidom to write out some xml using Node.toxml().

Is anybody aware of a method to achieve vec_msum functionality against a vector of float values? I'm quite new to SIMD, and although I think I'm starting to make sense of it - there are still a few p

I have a group having 2 different dates. The result that it outputs is like this: PeriodID-----GroupName-----Value 16-----FUEL-----200 15-----FUEL-----800 Now I want to subtract 200 from 800. and des

Which has more values, a 64-bit integer or a 64-bit float? My answer is that float has more value because its more accurate. But isn't integer is always greater than a float ?

I am looking to validate a field using php. The value to be entered in that field should be a float value. None other can be accepted. Not even integer. It should accept values like 5.2 or 4.1 and so

Hey guys pls i'm trying to retrieve values from my database using foreach but one of the fields/row in the table is an array which i serialized into the database. i have no problem retrieving this val

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 s

This question already has an answer here: why f is placed after float values? 5 answers I've defined a method with one parameter of type float shown below: public float getValue(float value){ r

#include <stdio.h> struct testStruct { int max; float y; float x; }; int main() { struct testStruct test; test.max = 50; test.y = 0.001; test.x = 0.05; printf(%i\n, test.max); printf(%i\n, t

Why does numeric_limits::min return a negative value for int, but positive values for e.g. float and double? #include<iostream> #include<limits> using namespace std; int main() { cout <

I'm having trouble printing out the values from my MySQLi query. Here is the db connection class that I am using. class db { public function __construct() { $this->mysqli = new mysqli('localhost',

Hi how do i have divs layout like this using CSS float and other methods. i am trying to avoid using tables and learn how to float divs properly. Thanks

I need to find maximum and minimum of 8 float values I get. I did as follows. But float comparisons are going awry as warned by any good C book! How do I compute the max and min in a accurate way. mai

How can I convert float to float option. Here is the function: let variance (list_ : float list) : float option = if List.length list_ < 2 then None else begin let average tmp_list = List.fold_left

What I'm trying to achieve is the difference between chapter 1 start time and chapter 2 start time and so on subtracting each chapter start time from the next in the array e.g. 00:05:57 - 00:01:03 = 0

Is an array possible to store int, string and float all at the same time? I have been seeing some array syntax but all starts with int array[] or string array[], is there a way which a array can store

I have the following css: .horizontal-holder1{ float:left; width:88px; height:98px; margin-bottom:15px; border:1px solid #bdbdbd; background-color:grey; } .white-holder{ width:88px; height:49px; backg

I have checked this similar question, but the suggestions did not solve my problem: Low precision and clamping when writing to floating point FBO. I am rendering a float image using a framebuffer obje

I have a list of dictionaries as follows: list = [ { 'a':'1' , 'b':'2' , 'c':'3' }, { 'd':'4' , 'e':'5' , 'f':'6' } ] How do I convert the values of each dictionary inside the list to int/float? So i

I'm running into an issue with python automatically rounding very small numbers (smaller than 1e-8) when subtracting an array from an single float. Take this example: import numpy as np float(1) - n

I have a stored procedure that has input params: @Latitude float , @Longitude float but when I pass these values: @Latitude=-28.640328248358344 ,@Longitude=153.61249352645871 the values being stor

i want to subtract values of two boundFields in gridview and show result in a template filed on run time .. Here is the code <asp:BoundField ControlStyle-Width=5% DataField=totalamount HeaderTe

My function returns a float value from an object. If the function cannot find a proper float value an error should be returned so i can deal with it properly in the code. My question is how to return

Possible Duplicate: Extra leading zeros when printing float using printf? I'm trying to get the output of this C program to have placeholder zeros, as the output of this program will be used as inpu

Is there a fast way to multiply values of a float array in C++, to optimize this function (where count is a multiple of 4): void multiply(float* values, float factor, int count) { for(int i=0; i <

>>> float(str(0.65000000000000002)) 0.65000000000000002 >>> float(str(0.47000000000000003)) 0.46999999999999997 ??? What is going on here? How do I convert 0.47000000000000003 to st

Calculating float values tclsh % expr 0.2+0.2 0.4 % expr 0.2+0.1 0.30000000000000004 % Why not 0.3?? Am i missing some thing. thanks in advance.

I have the following code embeded in a class.Whenever I run distToPoint it gives the error 'unsupported operand type(s) for -: 'NoneType' and 'float'' I don't know why it's returning with NoneType and

I have a textfield of Price. I want only Integer and Float values in it. I have done the Integer. But it is not accepting Float values like : 3110.6 Here is my Code DEMO JS: $(document).ready(function

Why DCT (Discrete Cosine Transform) is meant to operate between the pixel values -128 to 127 in JPEG compression? Why do level offsetting (subtracting by 128) is done on the pixel values before applyi

With the Teradata database, it is possible to load values of NaN, -Inf, and +Inf into FLOAT columns through Java. Unfortunately, once those values get into the tables, they make life difficult when wr

This question already has an answer here: Error subtracting floating point numbers when passing through 0.0 4 answers How dangerous is it to compare floating point values? 10 answers I test

#wrap{ border: 1px solid black; margin: 10px; float: left; width: 200px; height: 150px; } ul{ list-style: square; } <div id=wrap> MEH! </div> <ul> <li>Test</li> <li&

LibGDX is using float input values for some methods but I really don't get How I can set these values. For example; Sprite sprite = new Sprite(); sprite.setSize(0.9f, 0.4f); I don't think these value