We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › A little confused about MAX_FLOAT
Page Index Toggle Pages: 1
A little confused about MAX_FLOAT (Read 1974 times)
A little confused about MAX_FLOAT
Jul 30th, 2008, 9:07pm
 
I'm a little confused about how the MAX_FLOAT (and therefore also the MIN_FLOAT) value works. Is it a sort of "container" that stores the highest value found so far and then gets continually updated until the for loop reaches its end? Can there only be one MAX_FLOAT value per sketch?

Sorry if the wording doesn't make sense. I'm a complete newb to programming.
Re: A little confused about MAX_FLOAT
Reply #1 - Jul 30th, 2008, 9:46pm
 
MAX_FLOAT is the maximum possible value a float can have.. if you set a float to MAX_FLOAT then try to add any value to it, things will break.
Re: A little confused about MAX_FLOAT
Reply #2 - Jul 30th, 2008, 10:21pm
 
oooooooooooh I get it now. Thank you!
Re: A little confused about MAX_FLOAT
Reply #3 - Jul 31st, 2008, 8:52pm
 
One more question. For the following code

PImage mapImage;
Table locationTable;
int rowCount;

Table dataTable;
float dataMin = MAX_FLOAT;
float dataMax = MIN_FLOAT;

void setup() {
 size(640, 400);
 mapImage = loadImage("map.png");
 locationTable = new Table("locations.tsv");
 rowCount = locationTable.getRowCount();
 
 dataTable = new Table("random.tsv");

for (int row = 0; row < rowCount; row++) {
 float value = dataTable.getFloat(row, 1);
 if (value > dataMax) {
   dataMax = value;
}
if (value < dataMin) {
 dataMin = value;
}
}
}

the book says that by setting dataMin to MAX_FLOAT, "this ensures that dataMin will be replaced with the first value found in the table." How is this happening? Why doesn't dataMax get replaced with the first value found in the table?
Re: A little confused about MAX_FLOAT
Reply #4 - Jul 31st, 2008, 8:59pm
 
Any value you read in from the file will have a value less than MAX_FLOAT, and so get used. The same DOES also apply to dataMax, since any value will be greater than MIN_FLOAT.

If both dataMax and dataMin were started at 0, if all your data were less than 0, dataMax would still be set to 0, which would be wrong for your data.

The example is just showing how you can loop through some data to find the min/max values, by using the right starting points for the variables that hold the min/max values.

One other approach would be to have a special check to see if you're accessing the first value, and thne set min/max to that.
Re: A little confused about MAX_FLOAT
Reply #5 - Jul 31st, 2008, 11:04pm
 
oh ok so MAX_FLOAT establishes a sort of "roof" that the variable to which it is assigned cannot exceed?
Re: A little confused about MAX_FLOAT
Reply #6 - Jul 31st, 2008, 11:18pm
 
It doesn't set a roof, it is the roof by definition but you set it to that so the test will always succeed on the first value.

Here's an example.

Say you have a variable minVal and set it to 0.

You then iterate through your data which is 3.0,4.1,5.3" and for each value you check to see if it's lower than minVal, if it is, you set minVal to that value.

Now all your values are larger than 0, so minVal will remain 0. Now this is wrong, since from the data 3.0 4.1 and 5.3, 3.0 would be the minimum value.

So what you do, is instead of setting it to 0, you set it to MAX_FLOAT, which is the largest value any float could possibly be, so any value in your data file would be smaller than this.

So you test 3.0 against MAX_FLOAT which is actually the number 3.4028235 * 10^38 , so 3.0 is smaller than that, so minValue now becomes 3.0.. and so on.

It's just setting a target that you'll always beat, so you don't accidentally set a bad starting point.

More realistically, sometimes you could think "Ah, the number's will always be below 500, so start with it at 500.. but then you might get a data file where all the numbers are above 500, and you'll get the wrong answer.
Re: A little confused about MAX_FLOAT
Reply #7 - Aug 1st, 2008, 12:53am
 
Sweet! Ok now I really get it. This part "So you test 3.0 against MAX_FLOAT which is actually the number 3.4028235 * 10^38 , so 3.0 is smaller than that, so minValue now becomes 3.0.. and so on." really helped clear things up. Thank you.
Re: A little confused about MAX_FLOAT
Reply #8 - Sep 1st, 2009, 2:57pm
 
That was so helpful! This issue was my first stumbling block in Ben's book "Visualizing Data". It's a great read, for anyone interested in expressing, visually, any of the random info we have available to us through APIs, etc.
Re: A little confused about MAX_FLOAT
Reply #9 - Sep 1st, 2009, 3:11pm
 
I was interested to learn that MAX and MIN float values can vary from platform to platform in some programming languages (perhaps not for Processing), based on whether you're running a 32-bit, 64-bit, etc processor.

For example, when representing a number in binary with 32 bits, the highest number representable is 4,294,967,296. I think this is related to why my 32-bit Photoshop files can't exceed about 4 GB.
Page Index Toggle Pages: 1