Jump to content



struggling with math function

  • You cannot reply to this topic
10 replies to this topic

#1 winsrp   Members   -  Reputation: 103

Like
0Likes
Like

Posted 02 February 2012 - 09:20 PM

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.

Ad:

#2 boogyman19946   Members   -  Reputation: 165

Like
0Likes
Like

Posted 02 February 2012 - 09:50 PM



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.
"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^

#3 winsrp   Members   -  Reputation: 103

Like
0Likes
Like

Posted 02 February 2012 - 09:54 PM

hmm.... what would log2 be in programming?

#4 boogyman19946   Members   -  Reputation: 165

Like
1Likes
Like

Posted 02 February 2012 - 09:57 PM

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)
"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^

#5 winsrp   Members   -  Reputation: 103

Like
0Likes
Like

Posted 02 February 2012 - 10:00 PM

hmm... interesting results... thanks a lot... !!!

#6 Washu   Senior Moderators   -  Reputation: 2447

Like
0Likes
Like

Posted 02 February 2012 - 11:26 PM

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.
ScapeCode - Blog | SlimDX

#7 alvaro   Members   -  Reputation: 1689

Like
0Likes
Like

Posted 02 February 2012 - 11:31 PM

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


#8 Washu   Senior Moderators   -  Reputation: 2447

Like
0Likes
Like

Posted 02 February 2012 - 11:33 PM

View Postalvaro, on 02 February 2012 - 11:31 PM, said:

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.
ScapeCode - Blog | SlimDX

#9 alvaro   Members   -  Reputation: 1689

Like
0Likes
Like

Posted 02 February 2012 - 11:41 PM

View PostWashu, on 02 February 2012 - 11:33 PM, said:

View Postalvaro, on 02 February 2012 - 11:31 PM, said:

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.

Sir, you give me too little credit... I think I know what I am doing. Posted Image

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.

#10 Washu   Senior Moderators   -  Reputation: 2447

Like
0Likes
Like

Posted 02 February 2012 - 11:47 PM

View Postalvaro, on 02 February 2012 - 11:41 PM, said:

View PostWashu, on 02 February 2012 - 11:33 PM, said:

View Postalvaro, on 02 February 2012 - 11:31 PM, said:

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.

Sir, you give me too little credit... I think I know what I am doing. Posted Image
I never give anyone credit and assume they're idiots until they bother being clear with their meaning, Posted Image . 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.
ScapeCode - Blog | SlimDX

#11 alvaro   Members   -  Reputation: 1689

Like
0Likes
Like

Posted 02 February 2012 - 11:55 PM

The code is meant mostly as a joke or a curiosity.

A few years ago a friend and I were writing a go program using MCTS and one of our ideas slowed down our code too much because we needed to compute too many logarithms, so we came up with a crude approximation using the peculiarities of the IEEE-754 representation. I thought it was interesting that someone wants precisely this strange function.






We are working on generating results for this topic
PARTNERS