struggling with math function
Started by winsrp, Feb 02 2012 09:20 PM
10 replies to this topic
#1 Members - Reputation: 103
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.
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 Members - Reputation: 165
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
"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 ^.^
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 ^.^
#4 Members - Reputation: 165
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)
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 ^.^
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 ^.^
#6 Senior Moderators - Reputation: 2447
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
ScapeCode - Blog | SlimDX
#8 Senior Moderators - Reputation: 2447
Posted 02 February 2012 - 11:33 PM
alvaro, 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;
}
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
ScapeCode - Blog | SlimDX
#9 Members - Reputation: 1689
Posted 02 February 2012 - 11:41 PM
Washu, on 02 February 2012 - 11:33 PM, said:
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.
#10 Senior Moderators - Reputation: 2447
Posted 02 February 2012 - 11:47 PM
alvaro, on 02 February 2012 - 11:41 PM, said:
I never give anyone credit and assume they're idiots until they bother being clear with their meaning,
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
ScapeCode - Blog | SlimDX
#11 Members - Reputation: 1689
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.
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.


















