stephentyrone wrote:Taylor series are a bad choice for computationally approximating most functions...
however the 1st term is a pretty handy approximation for sin(x) where x is small
Moderators: phlip, Moderators General, Prelates
stephentyrone wrote:Taylor series are a bad choice for computationally approximating most functions...
TheChewanater wrote:Because I didn't even realize that...
Posi wrote:TheChewanater wrote:Because I didn't even realize that...
Either way, you are a horrible person.
TheChewanater wrote:Posi wrote:TheChewanater wrote:Because I didn't even realize that...
Either way, you are a horrible person.
...Why? Is that bad coding? Like I care...
Code: Select all
x += (right ? 1 : 0) - (left ? 1 : 0);
Posi wrote:TheChewanater wrote:Posi wrote:TheChewanater wrote:Because I didn't even realize that...
Either way, you are a horrible person.
...Why? Is that bad coding? Like I care...
Its so unclear! It burns my senses just reading about it.
Cosmologicon wrote:Would you accept a compromise?Code: Select all
x += (right ? 1 : 0) - (left ? 1 : 0);
That's pretty clear, right? No booleans were treated arithmetically in the making of this expression.
You, sir, name? wrote:This is one of those damned if you do, damned if you don't-type scenarios. If you use booleans arithmetically, Java programmers will send the code to thedailwtf. If you do what you just did, C programmers will send your code to thedailywtf.
Belial wrote:The sex card is tournament legal. And I am tapping it for, like, six mana.
You, sir, name? wrote:Posi wrote:TheChewanater wrote:Posi wrote:TheChewanater wrote:Because I didn't even realize that...
Either way, you are a horrible person.
...Why? Is that bad coding? Like I care...
Its so unclear! It burns my senses just reading about it.
How is that unclear? Any civilized person should know that in C and family, 'bool' is just a fancy way of saying 'int'.
Posi wrote:You, sir, name? wrote:Posi wrote:TheChewanater wrote:Posi wrote:TheChewanater wrote:Because I didn't even realize that...
Either way, you are a horrible person.
...Why? Is that bad coding? Like I care...
Its so unclear! It burns my senses just reading about it.
How is that unclear? Any civilized person should know that in C and family, 'bool' is just a fancy way of saying 'int'.
Because you have to think about what that code does. The if/else style may not be pretty, but you know right away what it does.
Code: Select all
SomeObjectType* object = dynamic_cast<SomeObjectType*>(anotherObject);
Code: Select all
namespace impl {
template <typename T> struct AutocastImpl
{
AutocastImpl(T* ptr):
_ptr(ptr)
{}
template<typename U> operator U* ()
{
return dynamic_cast<U*>(_ptr);
}
T* _ptr;
};
}
template<typename T>
impl::AutocastImpl<T> autocast(T* ptr)
{
return impl::AutocastImpl<T>(ptr);
}
Code: Select all
SomeObjectType* object = autocast(anotherObject);
0xBADFEED wrote:So obviously this little proof-of-concept only supports dynamic-cast of pointers but it would be trivial to extend it to support automatic static casting or any other 3rd-party cast routines and references in addition to pointers. Does something like this already exist? I don't immediately see any down-sides to it (other than it's non-standard and a possibly very mild performance hit) but I'm not really sure I'd ever use it in any real code. It's just such an easy utility and it seems like it cleans up casting code significantly. I'd be surprised if it doesn't already exist somewhere. Thoughts?
Code: Select all
auto *object = dynamic_cast<SomeObjectType*>(anotherObject);
Area Man wrote:C++0x :Code: Select all
auto *object = dynamic_cast<SomeObjectType*>(anotherObject);
Code: Select all
// imo, don't do something like this...
#define DCAST( x, y, z ) x* y = dynamic_cast<x*>(z)
// ...in code, looks like a function, isn't.
DCAST( SomeObjectType, object, anotherObject );
Area Man wrote:The new auto has been useful since gcc 4.4 (using -std=c++0x), and it won't be taken out of the standard, so I use it now.
If I recall correctly, the static/dynamic/const/reinterpret_cast notation was deliberately made explicit and "ugly" to be easier to spot in code than c-style casts (but I can't find a reference for it). Making it easy for a user to hide this kind of thing is contrary to that, and probably a bad thing.
Code: Select all
template<typename T> T* unconst(const T* v) { return const_cast<T*>(v);}
lulzfish wrote:Somewhere, I have a shell script called "crack". It is extremely useful, but only for a few seconds in some cases, and it would be more useful if Dolphin implemented it.
Have you ever downloaded one of those zips that has like a billions nested folders?
You get "sauce.zip".
You unzip it.
You go into the directory "sauce".
You go into the directory "src".
You go into the directory "2010-02-21".
You go into the directory "source".
HEY! There's the file I wanted! Fucking 4 levels in!
crack.sh is a simple script that 'cracks' open directories like coconuts, to acquire the delicious innards faster. It automatically moves everything out of a directory, then deletes the empty directory. Unfortunately, I don't know anything about permissions, and it's only useful every once in a while, so I've not developed it. But it seems like the kind of "duh" thing that should obviously be standard in all file managers. Like "Up" and like not having those stupid bread-crumbs. I should just write my own file manager.
Code: Select all
#!/bin/bash
# I'll get back to this later. It's a tool for cracking directories open.
if [ -d "$1" ]; then
mv $1/* $1/..
rmdir $1
else
echo "Argument should be a directory"
fi
lulzfish wrote:Code: Select all
#!/bin/bash
# I'll get back to this later. It's a tool for cracking directories open.
if [ -d "$1" ]; then
mv $1/* $1/..
rmdir $1
else
echo "Argument should be a directory"
fi
It probably doesn't preserve permissions or anything. It's just an automated "mv * ; rmdir". Obviously.
Code: Select all
#!/bin/bash
function print_documentation
{
##! Here's some documentation!
identifier=$1
# There's one space and one tab between all the square brackets below
searchstring="^[ ]*$identifier"
grep "$searchstring" $0 | sed "s/^[ ]*$identifier[ ]*//"
}
#£ Here's some different documentation... #£ ##!
print_documentation $1
Code: Select all
$ ./tmp.sh '##!'
Here's some documentation!
$ ./tmp.sh '#£'
Here's some different documentation... #£ ##!
Code: Select all
double sqrt(double toberooted, double guess){
double y = toberooted / guess;
if (y == guess){
return guess;
}
guess = (guess + y) / 2;
return sqrt(toberooted, guess);
}
Code: Select all
double invert_internal( function<double(double)> f, double target, double a, double b )
{
double test = 0.;
double middle = 0.;
do
{
middle = (a+b)/2.;
if (middle == a) return middle;
test = f((a+b)/2.);
if (test < target)
{
a = middle;
continue;
}
if (test > target)
{
b = middle;
continue;
}
} while (test != target)
return middle;
}
double invert_monotonic_function( function<double(double)> f, double target )
{
double a = 0.;
double interval = 1.;
while (f(a) > target)
{
a += interval;
interval *= -2.;
}
double b = 0.;
interval = 1.;
while (f(b) < target)
{
b += interval;
interval *= -2.;
}
return invert_internal( f, target, a, b );
}
Dr. Willpower wrote:This is not much of an accomplishment, but I've been trying to figure out how to sqrt() without google for over a month.
The algorithm I wrote:Code: Select all
double sqrt(double toberooted, double guess){
double y = toberooted / guess;
if (y == guess){
return guess;
}
guess = (guess + y) / 2;
return sqrt(toberooted, guess);
}
Code: Select all
derive ε f = \x → (f (x + ε) - f x) / ε
newton ε f = newt
where
f' = derive ε f
newt x | abs (f x) < ε = x
| otherwise = newt x'
where
x' = x - (f x / f' x)
squareRoot x = newton ε (\x' → x'^2 - x) 0
where ε = 0.00001
Code: Select all
squareRoot x eps = last $ takeWhile test $ iterate f (x/2)
where
f guess = (guess + x / guess) / 2
test guess = abs(x - guess^2) > eps^2
Code: Select all
squareRoot x eps = fst $ last $ takeWhile diffTest $ split $ takeWhile test $ iterate f (x/2)
where
f guess = (guess + x / guess) / 2
test guess = abs(x - guess^2) > eps^2
split a = zipWith (,) a $ tail a
diffTest (a,b) = a /= b
Berengal wrote:I quite like this one as a relatively quick-n-dirty square root:Code: Select all
derive ε f = \x → (f (x + ε) - f x) / ε
newton ε f = newt
where
f' = derive ε f
newt x | abs (f x) < ε = x
| otherwise = newt x'
where
x' = x - (f x / f' x)
squareRoot x = newton ε (\x' → x'^2 - x) 0
where ε = 0.00001
headprogrammingczar wrote:Berengal wrote:I quite like this one as a relatively quick-n-dirty square root:Code: Select all
derive ε f = \x → (f (x + ε) - f x) / ε
newton ε f = newt
where
f' = derive ε f
newt x | abs (f x) < ε = x
| otherwise = newt x'
where
x' = x - (f x / f' x)
squareRoot x = newton ε (\x' → x'^2 - x) 0
where ε = 0.00001
What language is that?
You, sir, name? wrote:headprogrammingczar wrote:Berengal wrote:I quite like this one as a relatively quick-n-dirty square root:Code: Select all
derive ε f = \x → (f (x + ε) - f x) / ε
newton ε f = newt
where
f' = derive ε f
newt x | abs (f x) < ε = x
| otherwise = newt x'
where
x' = x - (f x / f' x)
squareRoot x = newton ε (\x' → x'^2 - x) 0
where ε = 0.00001
What language is that?
Haskell. Leksah (et al.?) renders it that way. It's like APL, except it's reasonably readable.
TheChewanater wrote:You, sir, name? wrote:headprogrammingczar wrote:Berengal wrote:I quite like this one as a relatively quick-n-dirty square root:Code: Select all
derive ε f = \x → (f (x + ε) - f x) / ε
newton ε f = newt
where
f' = derive ε f
newt x | abs (f x) < ε = x
| otherwise = newt x'
where
x' = x - (f x / f' x)
squareRoot x = newton ε (\x' → x'^2 - x) 0
where ε = 0.00001
What language is that?
Haskell. Leksah (et al.?) renders it that way. It's like APL, except it's reasonably readable.
Readable? Yes. But, writable?
Code: Select all
derive epsilon f = \x -> (f (x + epsilon) - f x) / epsilon
Code: Select all
derive ε f = \x → (f (x + ε) - f x) / ε
RoadieRich wrote:Dr. Willpower wrote:This is not much of an accomplishment, but I've been trying to figure out how to sqrt() without google for over a month.
The algorithm I wrote:Code: Select all
double sqrt(double toberooted, double guess){
double y = toberooted / guess;
if (y == guess){
return guess;
}
guess = (guess + y) / 2;
return sqrt(toberooted, guess);
}
Is this guaranteed to terminate? I've been unable to find a case in which it won't (not that I've looked very hard), but doing == on two floats always seems risky to me.
Code: Select all
class DerivableFunc f where
evaluate :: (Fractional x) => f x x -> x -> x
derivative :: (Fractional x) => x -> f x x -> f x x
newtonSolve :: (Ord x, Fractional x, DerivableFunc f) => x -> f x x -> x -> x
newtonSolve epsilon f y = solve y -- y is as good a starting guess as anything
where solve guess
| abs (guess_val - y) <= epsilon = guess
| otherwise = solve (guess - (guess_val - y) / guess_slope)
where guess_val = evaluate f guess
guess_slope = evaluate (derivative 0.1 f) guess
-- So then you could do:
instance DerivableFunc (->) where
-- evaluate :: (Fractional x) => (x -> x) -> (x -> x) -- pretty simple
evaluate = id
-- derivative :: (Fractional x) => x -> (x -> x) -> (x -> x)
derivative epsilon f x = (f (x + epsilon) - f (x - epsilon)) / (2 * epsilon)
sqrt_1 = newtonSolve 0.001 (^2)
-- Or you could do:
data Polynomial x y = Polynomial [x] -- Polynomial [a,b,c,d,...] encapsulates a + bx + cx^2 + dx^3 + ...
instance DerivableFunc Polynomial where
-- evaluate :: (Fractional x) => Polynomial x -> x -> x
evaluate (Polynomial cs) x = sum $ map (uncurry (*)) $ zip cs $ iterate (*x) 1
-- evaluate :: (Fractional x) => x -> Polynomial x -> Polynomial x
-- we can ignore epsilon, 'cause we can get the exact answer
derivative _ (Polynomial []) = Polynomial []
derivative _ (Polynomial cs) = Polynomial $ map (uncurry (*)) $ zip (map fromIntegral [1..]) (tail cs)
sqrt_2 = newtonSolve 0.001 (Polynomial [0,0,1])
Code: Select all
enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵╰(ಠ_ಠ ⚠) {exit((int)⚠);}
phlip wrote:So I threw this together quickly:
stephentyrone wrote:I thought high-level languages were supposed to let you do more with less code?
Code: Select all
squareRoot num epsilon = newt 1
where newt x | abs (x*x-num) <= epsilon = x
| otherwise = newt x' where x' = x - (x*x-num) / (2*x)
Code: Select all
squareRoot num = sqrt num -- from standard library
mittfh wrote:I wish this post was very quotable...
flicky1991 wrote:In both cases the quote is "I'm being quoted too much!"
Code: Select all
enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵╰(ಠ_ಠ ⚠) {exit((int)⚠);}
(I was not entirely serious in my previous post)phlip wrote:But that's sacrificing genericity in the name of brevity.
I was trying to come up with something that'd be just as general, but without the sacrifices where they weren't necessary. Sqrt is just an example.
mittfh wrote:I wish this post was very quotable...
flicky1991 wrote:In both cases the quote is "I'm being quoted too much!"
Code: Select all
taylor epsilon f = calculate f 1 1
where calculate f n acc = (evaluate f 0) / acc : calculate (derivative epsilon f) (n + 1) (acc * n)
Code: Select all
enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵╰(ಠ_ಠ ⚠) {exit((int)⚠);}
fazzone wrote:Or you could use http://en.wikipedia.org/wiki/Fast_inverse_square_root (Probably less efficient, but entertaining anyway)
Not to mention even if both of those weren't true, it's also much less precise, which if you're really doing something where you need accuracy, you're better off with a bit slower but surer method.stephentyrone wrote:Yeah, sadly it turns out that the "fast" inverse square root is (a) no longer portable due to the extremely zealous adherence to aliasing rules in many current compiler optimizers, and (b) slower than just using the floating point hardware on (nearly) any computer built in the last 8 years, and even current-generation smartphones.
Users browsing this forum: No registered users and 9 guests