473,326 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

What does this function do?

#include <stdio.h>

float puzzle( float inp )
{
const float ths = 1.5F;
const long k = 21*76069667;

float a, b;
int c;

a = inp * 0.5F;
b = inp;
c = *( long *) &b;
c = k - ( c >1 ) ;
b = *( float *) &c;
b = b * ( ths - ( a * b * b ) ) ;

return 1/b;
}
Nov 17 '08 #1
3 2786
On Nov 17, 6:55*am, "c.lang.mys...@gmail.com"
<c.lang.mys...@gmail.comwrote:
#include <stdio.h>

float puzzle( float inp )
{
* const float ths = 1.5F;
* const long k = 21*76069667;

* float a, b;
* int c;

* a = inp * 0.5F;
* b = inp;
* c = *( long *) &b;
* c = k - ( c >1 ) ;
* b = *( float *) &c;
* b = b * ( ths - ( a * b * b ) ) ;

* return 1/b;

}- Hide quoted text -

- Show quoted text -
A very inaccurate square root that won't work on a 64-bit platform.
--
Fred
Nov 17 '08 #2
c.***********@gmail.com wrote:
#include <stdio.h>

float puzzle( float inp )
{
const float ths = 1.5F;
const long k = 21*76069667;

float a, b;
int c;

a = inp * 0.5F;
b = inp;
c = *( long *) &b;
The conversion has behavior that is undefined if b doesn't happen to
be aligned correctly to store a long integer; this is unlikely, but
entirely possible. The only thing that the standard guarantees about
the resulting pointer is that if it is converted back to float*, it
will compare equal to &b (6.3.2.3p7).

For an object of type float, "long" is not one of the types listed in
section 6.5p7; therefore, trying to access an object of type float
using an lvalue of type "long" violates a "shall" occuring outside of
a Constraints section - the behavior of this code is undefined.

Even if right hand side of that expression had precisely the behavior
that the author expected it to have, there would be no guarantee that
the resulting value is within the range of an 'int'. Therefore, when
that value is implicitly converted to 'int' before assigning it to c,
"... either the result is implementation-defined or an implementation-
defined signal is raised." (6.3.1.3p3).
c = k - ( c >1 ) ;
Even on a system where the value of c is precisely what the author of
this code probably expected it to be, there's no guarantee that c is
positive. That being the case, the result of the shift operation is
implementation-defined (6.5.7p5).
b = *( float *) &c;
This statement has all of the same problems as the calculation of 'c',
except that section 6.3.1.3p3 will not be an issue unless INT_MAX >
FLT_MAX; this is extremely unlikely, but is technically a possibility.

Therefore, the correct answer to your question is "this function could
do anything". Even if the undefined behavior could be avoided, the
correct answer would still depend upon which implementation was used
to translate and execute this code. You haven't identified the
implementation, so the question could not be answered, even in that
case.
Nov 17 '08 #3
On Nov 17, 2:55*pm, "c.lang.mys...@gmail.com"
<c.lang.mys...@gmail.comwrote:
#include <stdio.h>

float puzzle( float inp )
{
* const float ths = 1.5F;
* const long k = 21*76069667;

* float a, b;
* int c;

* a = inp * 0.5F;
* b = inp;
* c = *( long *) &b;
* c = k - ( c >1 ) ;
* b = *( float *) &c;
* b = b * ( ths - ( a * b * b ) ) ;

* return 1/b;

}
The return statement shows that the author of the posted code is
trying to be clever, but fails. The code will on some C
implementations set b to a rather imprecise approximation to sqrt (1 /
inp).in rather short execution time. It is fast by avoiding divisions
which tend to be rather slow. And what is the last thing it does? It
returns 1/b to return a bad approximation to sqrt (inp), using a
division and destroying all the speed advantage. Instead one should
return inp * b.

Nov 17 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
37
by: Bengt Richter | last post by:
ISTM that @limited_expression_producing_function @another def func(): pass is syntactic sugar for creating a hidden list of functions. (Using '|' in place of '@' doesn't change the picture...
7
by: Jonathan Fine | last post by:
Giudo has suggested adding optional static typing to Python. (I hope suggested is the correct word.) http://www.artima.com/weblogs/viewpost.jsp?thread=85551 An example of the syntax he proposes...
92
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption?...
70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
15
by: robert maas, see http://tinyurl.com/uh3t | last post by:
Here's the source: #include <stdio.h> #include <errno.h> main () { char* str = "9999999999"; long long int llin; char* endptr; /* Set by strtoll */ int nch; errno = 0; llin = strtoll(str,...
92
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.