I wonder whether there is a faster way (faster then the obvious one a=a*a) to square the content of a vector of floats. I remember that bit shifting could be used somehow, but I'm not sure whether this works for floats. Any suggestions on how to achieve this are welcome (perhaps with the code).
Thanks

Recommended Answers

All 2 Replies

float a;
cin >> a;
a = a*a;

well, this is the fastest way I am aware of, perhaps you could make it a tiiiiiiiiiiny bit faster with
a *= a;
but probably most compilers will optimise this anyway.

Bit shifting is about integers, and you can't use it (I think) to square an integer

x >>= i; is equivalent to x *= 2^i (^ denoting power)

hope this helps

if you have a vector of floats you might want to use the for_each function in the algorithms header.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.