473,511 Members | 16,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2791
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
33327
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
2571
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
2185
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
6332
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
8809
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
9908
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
13785
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
6673
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
8987
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
6118
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
7245
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7085
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5069
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1577
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
449
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.