convert float to int
Blitz3D Forums/Blitz3D Beginners Area/convert float to int
| ||
Wasn't there a function that allows to convert a float to an int in a binary way (like poking a float to a bank, then read it out as an int) OR maybe it was float to 4-byte string. I just can't find it right now in the docs, sorry. I am currently using the bank method I just described, but I think it would be more elegant to use the directly converting function. |
| ||
I take it, it's not as simple as saying:x% = y# ? |
| ||
No, that's not what I mean. try this: a#=3.14159 ba=createbank(4) pokefloat ba,0,a# a2%=peekint(ba,0) print a2% pokeint ba,0,a2% a#=peekfloat(ba,0) print a# a2% is the binary equivalent of a#, not the numeric equivalent. |
| ||
Oh right, i get what you mean now :o) Sorry i can't be of any help on that one. |
| ||
thanks anyway. |
| ||
The C++ equivilent of what you want to do is called a "reinterpret_cast". There's no way to do this in Blitz3D without using files or banks. |
| ||
ok i see, thanks. |
| ||
umm, can a float be converted to pure binary? True binary is in whole numbers (powers of 2), not fractions. |
| ||
:) Never heared of 0.0001 ? Seriously, in the end of the day everything is stored binary in your machine. |
| ||
True, everything is stored in binary. Certainly, 0.0001 is represented as binary in a computer memory in a particular format that works out as a float, but I am not understanding what was meant in the original question. |
| ||
I believe when floats are represented in binary, it uses bits to represent 1/2 , 1/4, 1/8 etc etc. That's what i was taught in college. I never did figure out how they stored 0.1 though... |
| ||
floats are implemented in various way, one is, as the name says, with a floating point. So there is a number , like 134523, and a point index, eg. 4, the result would then be 1345.23 cause the point has to be set after the forth digit. A binary piece of ram (eg 32 bits) would then use eg. 5 bits to store the point position and the rest for the value without a point. thought my question was clear. when there is a float in the ram, and you peek it as an int, that's the binary integer equivalent of the float. I needed this because when you peek random ram adresses as float, blitz will oftenly return NAN (not a number), and when you compare this with eg. zero, it will say NAN is zero, although it may be something completely diffrent that simply was not a float. So I needed to compare in a binary way (see the "how to hack properties" thread in the tutorials section). |
| ||
floats are implemented in various way, one is, as the name says, with a floating point. So there is a number , like 134523, and a point index, eg. 4, the result would then be 1345.23 cause the point has to be set after the forth digit. A binary piece of ram (eg 32 bits) would then use eg. 5 bits to store the point position and the rest for the value without a point. Not sure where you got that idea from. Floats on PC's certainly aren't stored as 'simply' as that. Take a look: http://en.wikipedia.org/wiki/IEEE_floating-point_standard#Single-precision_32_bit |
| ||
Well I said it's one way. Bysides, what sounds so educted on that wiki page isn't that far from what I said, I only simplified it in respect to this thread. You may have realized that the basic method is the same: Normalised numbers use exponent and fraction. If you cut off all the educted sounding phrases you'll realize they're only cooking with water too. But why should we make things simple if they can be complicated, hu? |
| ||
... NAN (not a number), and when you compare this with eg. zero, it will say NAN is zero ... Not quite. It's just that comparing NaN to zero yields NaN, which Blitz is interpreting as a true value when deciding whether the IF condition should succeed. If Sqr(-1) = 0 And Sqr(-1) = 12345 Then Print "told you so" Here are some partially-tested "special case float" testing functions I just wrote: Function isNaN(n#) Return n = 0 And n = 12345 End Function Function isInf(n#) Return n / 1000 = n And (Not isNaN(n)) End Function Function isPosInf(n#) Return isInf(n) And Sgn(n) = 1 End Function Function isNegInf(n#) Return isInf(n) And Sgn(n) = -1 End Function EDIT: fixed isInf() so as not to report true on NaN EDIT: fixed isInf() to work with huge floats |
| ||
thanks, very interesting. Some time ago I already discovered that blitz has some relations to quantum physics with multiple realities (where nan is zero and one in the same time :) ) |
| ||
Note that Blitz's handling of NaN is completely incorrect according to the wikipedia page on NaN: A NaN does not compare equal to any floating-point number or NaN, even if the latter has an identical representation. I've submitted a bug report. |
| ||
Umm, has anybody actually checked out how Blitz stores floats internally? It seems to me that once you know that, you should be able to obtain the solution desired. |
| ||
Blitz must store floats in the native format used by the CPU, just as every other PC program does. That's why I posted a link to the format used and pointed out it was different to the method jfk mentioned. I was only trying to help. |
| ||
Makes sense. After all, why re-invent the wheel? And, as I understand it, Blitz was created in C (or a variation thereof), so however the compiler does a float, that would seem to be the format we would reasonably expect. |
| ||
big10p, ok, thanks. Although, decoding a float based on that IEEE standard is an overkill, compared to poking it as a float to a bank, then reading it out as an int. So I let the CPU do the convertion. |
| ||
a#=3.14159 ba=createbank(4) pokefloat ba,0,a# a2%=peekint(ba,0) print a2% pokeint ba,0,a2% a#=peekfloat(ba,0) print a# To get the numeric equivelant, why not just read it out as a float and convert it to an int, like this: a#=3.14159 ba=CreateBank(4) PokeFloat ba,0,a# a2%=PeekFloat(ba,0) Print a2% ... or am I misunderstanding the question? |
| ||
yes, misunderstanding. The numeric equivalent was NOT needed, it's the binary equivalent I needed. Again, run this: a#=3.14159 ba=createbank(4) pokefloat ba,0,a# print "numeric equivalent integer:" print int(peekfloat(ba,0)) print "binary equivalent integer:" print peekint(ba,0) I need this to compare ints that have been "peeked" from RAM using the kernel32.dll's function "RTLMoveMemory" to some floats that are known. I am searching for a float that was used in BrushAlpha, but comparing floats will incorrectly return true in some cases, when one of them is an invalid float, eg. NAN. So both need to be ints to be able to compare them in a way that works. |
| ||
but comparing floats will incorrectly return true in some cases Not if you use a comparison function that isn't broken! Run my test program below to show the results of several comparisons. Add more to the bottom of the file if you want, just make sure you leave Data secret_data_end_value at the bottom. To explain the function a bit, a<>-b is false if either is NaN, but also false if both are zero, so I had to add the extra (a=0 And a<>1) to allow for the case where a=b=0. The a<>1 is there to make sure that a is in fact really 0 and not NaN. Note that I went out of my way to ensure Infinity==Infinity and -Infinity==-Infinity. :) |
| ||
I agree, NAN may be handled not correctly internally. Noless the power of Bitz3D outweights this bug by far. I never had any serious problems with it. At the other hand I'm sure when I would use C++ to code my FPS game engine, I'd stuck somewhere in the beginning. Once I had a frame per second counter for a test program and when it was running very fast then the counter said FPS: infinite - well that was rather a funny sideeffect than a bug. Usualy you will not be confronted with NANs etc. as long as you compare floats that are created by blitz. |
| ||
I am searching for a float that was used in BrushAlpha, but comparing floats will incorrectly return true in some cases, when one of them is an invalid float, eg. NAN. So both need to be ints to be able to compare them in a way that works. So are you going to use my proper_float_equality_test() or keep with the reinterpret_cast to ints strategy? :) |
| ||
the reinterpretation of the bank content is in fact much simpler. But your functions are useful for some special cases. So I saved this thread. thanks for sharing :) |
| ||
Ugh. I hope no one ever has to touch that code again. Including you 6 months later! :) |
| ||
And this was "Beginner's" thread? |
| ||
:) yes it started as a beginner thread and evolved slowly. |