Public Group

# struggling with math function

This topic is 2818 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

## 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.

##### Share on other sites
hmm.... what would log2 be in programming?

##### Share on other sites
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)

##### Share on other sites
hmm... interesting results... thanks a lot... !!!

##### Share on other sites
You should perhaps try and describe what you're trying to achieve with this "function", where the numbers come from, and why...

##### Share on other sites
This is not totally kosher, but it might work:
float f(float x) { return 138.0f - *reinterpret_cast<int *>(&x)/8388608.0f; } 

##### Share on other sites

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.

##### Share on other sites

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

##### Share on other sites

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

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 9
• 11
• 15
• 21
• 26