struggling with math function

Started by
9 comments, last by alvaro 12 years, 2 months ago
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.
Advertisement



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 :D I'm sure there's someone more skilled in math than I am that can provide a better answer though.

Yo dawg, don't even trip.

hmm.... what would log2 be in programming?
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)

Yo dawg, don't even trip.

hmm... interesting results... thanks a lot... !!!
You should perhaps try and describe what you're trying to achieve with this "function", where the numbers come from, and why...

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.


[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. smile.png

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. smile.png
[/quote]
I never give anyone credit and assume they're idiots until they bother being clear with their meaning, smile.png . 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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

This topic is closed to new replies.

Advertisement