473,836 Members | 1,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Most negative double value

What's the most negative double value I can get? Is it always
guaranteed to be -DBL_MAX? If so, why (or rather, where)?

Thanks.

Nov 14 '05
29 16903
> > >>#include <stdio.h>
>#define p(s) printf(#s" endian")
>int main(void){int v=1;*(char*)&v? p(Little):p(Big );return 0;}
You are aware that this is nonsense?

If not, then think about it for five minutes before you respond.


Please explain...


Do you think that bigendian and littleendian are the only possibilities?
Could the char that you are reading consist completely of padding bits
in the int, in which case you could get two different results when you
run the program twice?


Do you think that "endianness-detector" is the only possibility
for the purpose of this program? It could in fact be a random
greeting program in Martian, or a pedant-trapper, or a
compiler test, or pretty much anything really.
Nov 14 '05 #11
Christian Bau wrote:
Do you think that bigendian and littleendian are the only possibilities?
No, but I believe that are the most common ones..
Could the char that you are reading consist completely of padding bits
in the int, in which case you could get two different results when you
run the program twice?


I think if I change *(char*)&v to *(char*)&v==1 I can rid of it.. Or
don't I??

PS If the code below does not work (and I have come across it many
times, in books and the Inet), then what should??

--
#include <stdio.h>
#define p(s) printf(#s" endian")
int main(void){int v=1;*(char*)&v? p(Little):p(Big );return 0;}

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
Nov 14 '05 #12
Dan Pop wrote:
In <c1**********@n ews.apple.com> Peter Ammon <pe*********@ro cketmail.com> writes:

What's the most negative double value I can get? Is it always
guaranteed to be -DBL_MAX?

Yes.

If so, why (or rather, where)?

Because the standard uses a sign-magnitude model for the floating point
numbers. 5.2.4.2.2 in C99. Sorry, but the text is next to impossible
to quote in plain text format, without using some special convention,
a la TeX and friends.

Dan


There it is! Thanks.

-Peter

--
Pull out a splinter to reply.
Nov 14 '05 #13
In article <c1***********@ ulysses.noc.ntu a.gr>,
Papadopoulos Giannis <ip******@inf.u th.gr> wrote:
Christian Bau wrote:
Do you think that bigendian and littleendian are the only possibilities?


No, but I believe that are the most common ones..
Could the char that you are reading consist completely of padding bits
in the int, in which case you could get two different results when you
run the program twice?


I think if I change *(char*)&v to *(char*)&v==1 I can rid of it.. Or
don't I??

PS If the code below does not work (and I have come across it many
times, in books and the Inet), then what should??


The pragmatic solution is to add an appropriate #define into some header
file. Also helpful is to write code that doesn't depend on endianness in
the first place: For example, to write a value x up to 2^32 to a file,
write the bytes (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x &
0xff and you will get the same results everywhere.

C doesn't even say that there is such a thing as a "byte ordering": If
for example int has 16 bit, char has 8 bit, and sizeof (int) == 2, then
the 16 bits of an int could be mapped in any possible permutation to the
16 bits in an array of two chars.
Nov 14 '05 #14
In article <84************ **************@ posting.google. com>,
ol*****@inspire .net.nz (Old Wolf) wrote:
>>#include <stdio.h>
>>#define p(s) printf(#s" endian")
>>int main(void){int v=1;*(char*)&v? p(Little):p(Big );return 0;}
>
>
> You are aware that this is nonsense?
>
> If not, then think about it for five minutes before you respond.

Please explain...


Do you think that bigendian and littleendian are the only possibilities?
Could the char that you are reading consist completely of padding bits
in the int, in which case you could get two different results when you
run the program twice?


Do you think that "endianness-detector" is the only possibility
for the purpose of this program? It could in fact be a random
greeting program in Martian, or a pedant-trapper, or a
compiler test, or pretty much anything really.


If you read the next reply of the original poster, you will find that it
is an "endianness-detector".
Nov 14 '05 #15
Christian Bau wrote:
The pragmatic solution is to add an appropriate #define into some header
file. Also helpful is to write code that doesn't depend on endianness in
the first place: For example, to write a value x up to 2^32 to a file,
write the bytes (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x &
0xff and you will get the same results everywhere.
Yes, this is right if I am writing robust code to run everywhere... On
the other hand this is a really simple (and maybe stupid) program that
tries to find machine endianess and is part of my signature...
C doesn't even say that there is such a thing as a "byte ordering": If
for example int has 16 bit, char has 8 bit, and sizeof (int) == 2, then
the 16 bits of an int could be mapped in any possible permutation to the
16 bits in an array of two chars.


OK, but all the great minds of processor design have come to the
conclusion that the 16bit integer will be represented in either big
endian or little endian...

PS even with padding bits, I think *(char*)&v==1 should do the trick...
comments?

--
#include <stdio.h>
#define p(s) printf(#s" endian")
int main(void){int v=1;*(char*)&v? p(Little):p(Big );return 0;}

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
Nov 14 '05 #16

On Mon, 23 Feb 2004, Papadopoulos Giannis wrote:

Christian Bau wrote:
The pragmatic solution is [...]
Yes, this is right if I am writing robust code to run everywhere... On
the other hand this is a really simple (and maybe stupid) program that
tries to find machine endianess and is part of my signature...


I.e., it's not pragmatic or correct. Which will attract attention
in comp.lang.c, because that's the kind of thing we do here. :) If
you don't want comments on your sig, then you should not post your
sig to comp.lang.c.
C doesn't even say that there is such a thing as a "byte ordering": If
for example int has 16 bit, char has 8 bit, and sizeof (int) == 2, then
the 16 bits of an int could be mapped in any possible permutation to the
16 bits in an array of two chars.


OK, but all the great minds of processor design have come to the
conclusion that the 16bit integer will be represented in either big
endian or little endian...


Depends on what you mean by "great minds." I think the PDP-11
has been brought up as a (32-bit) counter-example before... or was
it the PDP-7? I don't know. And anyway, comp.lang.c doesn't care
about processor design, we care about C programming.
PS even with padding bits, I think *(char*)&v==1 should do the trick...
comments?


Wrong. Assuming 'v' is an int, you're invoking undefined behavior
by trying to look at its bits as if they represented a valid 'char'
value. So anything can happen, including nasal demons or wrong
answers.

-Arthur
Nov 14 '05 #17
Arthur J. O'Dwyer wrote:
I.e., it's not pragmatic or correct. Which will attract attention
in comp.lang.c, because that's the kind of thing we do here. :) If
you don't want comments on your sig, then you should not post your
sig to comp.lang.c.
I've never said I do not want comments on what I'm writing.. Comments
are always acceptable...
Wrong. Assuming 'v' is an int, you're invoking undefined behavior
by trying to look at its bits as if they represented a valid 'char'
value. So anything can happen, including nasal demons or wrong
answers.

-Arthur


For a 32-bit machine would

union boo {
unsigned int i;
unsigned char c[4];
};

union boo a;

and looking at a.c[0]

be more acceptable??

--
#include <stdio.h>
#define p(s) printf(#s" endian")
int main(void){int v=1;*(char*)&v? p(Little):p(Big );return 0;}

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
Nov 14 '05 #18

On Mon, 23 Feb 2004, Papadopoulos Giannis wrote:

Arthur J. O'Dwyer wrote:
I.e., it's not pragmatic or correct. Which will attract attention
in comp.lang.c, because that's the kind of thing we do here. :) If
you don't want comments on your sig, then you should not post your
sig to comp.lang.c.


I've never said I do not want comments on what I'm writing.. Comments
are always acceptable...


Well then, stop trying to justify yourself. ;-) ;-)
Wrong. Assuming 'v' is an int, you're invoking undefined behavior
by trying to look at its bits as if they represented a valid 'char'
value. So anything can happen, including nasal demons or wrong
answers.


For a 32-bit machine would

union boo {
unsigned int i;
unsigned char c[4];
};

union boo a;

and looking at a.c[0]
be more acceptable??


Not from the standard's point of view. Let's assume that by
"32-bit machine" you mean "an implementation on which 'CHAR_BIT'
is 8 and 'sizeof(int)' is 4." Then it is *still* possible that
INT_MAX could be 32767 or 65535 (I think) or some other number,
and a.c[0] could be composed entirely of padding bits and thus
irrelevant to the "endianness " of the machine (whatever that
means in a padding-bits context).

Actually, it would be a *little* more "acceptable "; now that
you've switched to 'unsigned char', the Standard guarantees that
'unsigned char' has no trap representations . Thus what you have
now is either implementation-defined or unspecified behavior,
depending on your interpretation of the Standard (I think); but
not undefined behavior, as it was before.

Anyway, the basic point I guess is that you haven't fully defined
what you mean by "endianness ." Sure, there are two or three obvious
"endian" cases, but how do you classify the big gray area including
padding bits and weird bit orders?
If you assume as a prerequisite that the implementation *must*
have either the 1234 or the 4321 byte order, and no padding bits,
then your program works perfectly. But standard C doesn't make that
assumption -- it makes different assumptions such that it is actually
impossible to determine whether a given 'int' is big- or little-endian
in a portable fashion. (I'm fairly sure that's right -- something
like the Halting Problem probably applies somehow.)

HTH,
-Arthur

Nov 14 '05 #19
In article <c1***********@ ulysses.noc.ntu a.gr>,
Papadopoulos Giannis <ip******@inf.u th.gr> wrote:
Christian Bau wrote:
The pragmatic solution is to add an appropriate #define into some header
file. Also helpful is to write code that doesn't depend on endianness in
the first place: For example, to write a value x up to 2^32 to a file,
write the bytes (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x &
0xff and you will get the same results everywhere.


Yes, this is right if I am writing robust code to run everywhere... On
the other hand this is a really simple (and maybe stupid) program that
tries to find machine endianess and is part of my signature...
C doesn't even say that there is such a thing as a "byte ordering": If
for example int has 16 bit, char has 8 bit, and sizeof (int) == 2, then
the 16 bits of an int could be mapped in any possible permutation to the
16 bits in an array of two chars.


OK, but all the great minds of processor design have come to the
conclusion that the 16bit integer will be represented in either big
endian or little endian...

PS even with padding bits, I think *(char*)&v==1 should do the trick...
comments?


No, consider a situation where:

char is 8 bits
int is 8 padding bits, followed by 24 value bits

The value of the expression (*(char*)&v) could be anything at all, so
comparing it to 1 is completely useless.

Nov 14 '05 #20

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

Similar topics

0
1898
by: Danny Winslow | last post by:
I get an unexpected result when I add two negative numbers in PHP on SPARC/Solaris 8. The same program works fine on Intel/Linux. I'm using PHP 4.3.1 on both systems. Here is my program, test.php. <?php $a = 0x81234567; $b = 0xF0000002; printx($a, $b);
2
4959
by: Richard van Denzel | last post by:
Hi All, I've defined a table (m) with an double field (bedrag) in it and when I execute the next statement: INSERT INTO m (bedrag) VALUES('-7000,00'); the value get stored correctly. I've written a PHP-script which does the following:
4
2148
by: Charles A. Lackman | last post by:
Hello, I have some textboxes that are doing addition: Dim AString As Double AString = Convert.ToDouble(TextBox1.Text) - Convert.To(DoubleTextBox2.Text) TextBox3.Text = AString.ToString("#,##0.00;(#,##0.00);0.00") When the result is negative it is displayed as "(46)" or whatever the value is.
16
5097
by: JKop | last post by:
Take a class like the following: class Finger { public: double length; }
5
6126
by: Subrahmanyam Arya | last post by:
Hi Folks , I am trying to solve the problem of reading the numbers correctly from a serial line into an intel pentium processor machine . I am reading 1 byte and 2byte data both positive and negative quantities . Can any one tell me what are the things i should bear in mind .
11
2399
by: John | last post by:
Hi, I encountered a strange problem while debugging C code for a Windows-based application in LabWindows CVI V5.5, which led me to write the test code below. I tried this code with a different compiler and got the same erroneous result on two different PCs (with OS Win98 & Win98SE), so it appears to be a problem with ANSI C. I thought that negative double variables could be compared as easily and *reliably* as integers, but apparently...
2
11278
by: Angus Comber | last post by:
Hello Sorry this really is a newbie question! I am doing some floating point arithmetic and calculating the time difference between two dates. The date values being comparied are actually double values. I was going to compare the dates by calculating the difference betwen the two numbers. But sometimes you get a -ve and sometimes a +ve value. I want to disregard a small variance eg of up to two minutes but what is best way to...
2
1481
by: Robert | last post by:
dear sir: I have read the c++ primer plus 5 book, and do the chapter 12 programming exercises question 5, but the average_wait have negative result. I don't understand why? here is the code, thank you anyone replay me.:) /************************* customer.h *************************/ #ifndef CUSTOMER_H_ #define CUSTOMER_H_
8
4257
by: mdeh | last post by:
Hi Everyone, Tried to post via Google..which once again seems to be fritzed...so please excuse if 2 posts show up. I am trying to understand why I am not getting a negative value back, using my version of atof given the argument "-36.63" double atof ( char *s){
0
9668
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10843
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10546
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10589
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9371
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5648
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4448
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 we have to send another system
2
4015
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.