So I need a function that when I give it
X = 1024 it returns Y = 1
X = 512 it returns Y = 2
X = 256 it returns Y = 3
if i give 768 it should give 1.5
if I give him 1 it should return 11
I have a headache now, and can't think much, so I'm asking for help. give it a try and let me know.
struggling with math function
I'm not sure if, given the set of values [1024, 768, 512, 256] it's possible to create a non-trivial function to make it the way you want, mainly because you want 768 to be 1.5. I mean, 256, 512, 1024 is growing exponentially like so:
f(x) = 2^x
where
f(8) = 256
f(9) = 512
f(10) = 1024
but f(9.5) is not 768 but about 724. You could try
f(x) = 11 - log2 ( x ) which comes pretty close I think
f(1024) = 1
f(756) = 1.4377
f(512) = 2
f(256) = 3
f(1) = 11
That's about as close as I can think of I'm sure there's someone more skilled in math than I am that can provide a better answer though.
Basically it's log with base 2 in math (I don't know how to denote log base in text). So if you were using log or log10 from cmath, you would write:
log(x)/log(2)
log(x)/log(2)
You should perhaps try and describe what you're trying to achieve with this "function", where the numbers come from, and why...
This is not totally kosher, but it might work:
float f(float x) {
return 138.0f - *reinterpret_cast<int *>(&x)/8388608.0f;
}
This is not totally kosher, but it might work:
float f(float x) {
return 138.0f - *reinterpret_cast<int *>(&x)/8388608.0f;
}
If you want it as an integer to truncate the decimal, use static_cast.
[quote name='alvaro' timestamp='1328247106' post='4909014']
This is not totally kosher, but it might work:
float f(float x) {
return 138.0f - *reinterpret_cast<int *>(&x)/8388608.0f;
}
If you want it as an integer to truncate the decimal, use static_cast.
[/quote]
Sir, you give me too little credit... I think I know what I am doing.
EDIT: Perhaps I owe you a bit of an explanation. I am taking the bit pattern that forms the float, reading it back as an integer, and dividing by 2^23 to get the exponent. The magic number 138 is the offset in the exponent (127) plus 11. The bits that used to be the mantissa of the float allow us to do linear interpolation in between powers of 2. This seems to be exactly the behavior the OP wanted. The non-kosher part is that this code breaks strict-aliasing rules, as reported by g++. So it might mess with some compiler optimizations.
[quote name='Washu' timestamp='1328247227' post='4909016']
[quote name='alvaro' timestamp='1328247106' post='4909014']
This is not totally kosher, but it might work:
float f(float x) {
return 138.0f - *reinterpret_cast<int *>(&x)/8388608.0f;
}
If you want it as an integer to truncate the decimal, use static_cast.
[/quote]
Sir, you give me too little credit... I think I know what I am doing.
[/quote]
I never give anyone credit and assume they're idiots until they bother being clear with their meaning, . I figured out what you were doing though, a combination of magical numbers combined with taking the "integral representation" of a floating point value (which assumes IEEE floats btw) to produce the desired result. It does work, although the behavior is implementation defined at best, thus I retract my static_cast statement.
Edit: Since I realize some people might take the idiots comment the wrong way (although it's probably true) let me explain: "Magic code" in for beginners is generally frowned upon.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement