Thread: Strange arithmetic results

  1. #16
    Registered User
    Join Date
    Jul 2011
    Posts
    7
    Salem:
    The compiler I´m using seems to be an Oracle-Tuxedo compiler. The makefile seems to refer to a compiler in the Tuxedo 8.1 directory. The application I´m coding is a Tuxedo application running on Unix.

    Everyone else:
    I thought perhaps this could be a type-related problem, so I played around with explicit casts, to no avail. That is, I made sure everything was a regular int, then I made sure everything was a long. But the results were always the same. Also, like Salem, I believe the results of the multiplication do not reflect a loss-of-precision error. The operands and the result of the calculation are not large enough to warrant any precision loss anyway.

    I guess that for the lack of a better explanation, I´m ready to accept the "...your compiler is junk" answer.

    Thanks

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Does the compiler have the equivalent of -S in GCC?
    Code:
    $ cat foo.c
    #include<stdio.h>
    long int a = 99;
    long int b = 42;
    void foo()
    {
      long int c = a * b;
      printf("%ld\n",c);
    }
    $ gcc -S -c foo.c
    $ cat foo.s
    	.file	"foo.c"
    .globl a
    	.data
    	.align 8
    	.type	a, @object
    	.size	a, 8
    a:
    	.quad	99
    .globl b
    	.align 8
    	.type	b, @object
    	.size	b, 8
    b:
    	.quad	42
    	.section	.rodata
    .LC0:
    	.string	"%ld\n"
    	.text
    .globl foo
    	.type	foo, @function
    foo:
    .LFB0:
    	.cfi_startproc
    	pushq	%rbp
    	.cfi_def_cfa_offset 16
    	movq	%rsp, %rbp
    	.cfi_offset 6, -16
    	.cfi_def_cfa_register 6
    	subq	$16, %rsp
    	movq	a(%rip), %rdx
    	movq	b(%rip), %rax
    	imulq	%rdx, %rax
    	movq	%rax, -8(%rbp)
    	movl	$.LC0, %eax
    	movq	-8(%rbp), %rdx
    	movq	%rdx, %rsi
    	movq	%rax, %rdi
    	movl	$0, %eax
    	call	printf
    	leave
    	ret
    	.cfi_endproc
    .LFE0:
    	.size	foo, .-foo
    	.ident	"GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
    	.section	.note.GNU-stack,"",@progbits
    If you can create a pair of functions with the offending line, one which works and the other doesn't, then you have something pretty solid for a potential bug report.

    But the real test would be to go through the generated assembler to see what is really going on.
    Really small code samples make this a relatively easy prospect.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange test results..
    By kwikness in forum C Programming
    Replies: 6
    Last Post: 12-08-2007, 05:47 PM
  2. Strange Results from cout
    By IdioticCreation in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2007, 05:09 AM
  3. Function keys and strange results
    By divineleft in forum Linux Programming
    Replies: 3
    Last Post: 11-12-2006, 06:39 AM
  4. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  5. mod arithmetic in C++
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 01-10-2002, 12:59 PM