|
|
Up |
|
|
  |
Author: Ron FordRon Ford Date: Sep 16, 2008 18:42
I've been sharpening up my C syntax, which has differing standards than
fortran. Within the context of a compare routine for qsort, a question
came up about when the difference between two integers busts the datatype.
I wanted to confirm that it happens about one eighth of the time, and to
that end, I wrote the following prog:
implicit none
integer, parameter :: i13 = selected_int_kind(13)
integer(i13), parameter:: i_max = 63
integer(i13), parameter:: i_tot = i_max*2 +1
! integer(i13), parameter:: sides = 8
integer(i13), parameter:: sides = i_tot
integer(i13), parameter:: trials = 10
integer(i13), dimension(trials)::C
integer(i13), dimension(trials)::F
integer(i13):: b, i, &
diff2, c3, c4
real:: harvest, diff, t3, t4
! seed random num generator
CALL init_random_seed
|
| Show full article (3.21Kb) |
|
| | 15 Comments |
|
  |
|
|
  |
Author: Ron FordRon Ford Date: Sep 16, 2008 22:43
On Tue, 16 Sep 2008 20:45:39 -0700 (PDT), e p chandler posted:
>> integer(i13), parameter:: i_max = 63
>> integer(i13), parameter:: i_tot = i_max*2 +1
>
>> integer(i13), parameter:: sides = i_tot
>> integer(i13), parameter:: trials = 10
>>
>> integer(i13), dimension...
|
| Show full article (4.40Kb) |
| no comments |
|
  |
Author: glen herrmannsfeldtglen herrmannsfeldt Date: Sep 16, 2008 23:36
Ron Ford wrote:
> I *do* have off by one bug, but I think we're both wrong at this point, as
> your classical way to roll a die has uneven outcomes.
All will have slightly uneven outcome unless i_tot evenly
divides the appropriate generator value, often the number
of possible return values and often a power of two. But...
> call random_number(harvest)
> b = min(max(1,ceiling(harvest*sides)),sides)
is different that the previously suggested:
>> b = -i_max + int( harvest * i_tot )
ceiling(harvest*sides) mostly returns a value between
1 and sides, with an extremely low probability of zero.
max(1,ceiling(harvest*sides)) the min doesn't do anything.
removes even that small probability. sides=t_tot=2*i_max+1,
the result is between 1 and 2*_max+1.
Later you subtract i_max, so between 1-imax and imax+1.
It seems you don't have bounds checking on, and you don't
notice the counts in k(i_max+1)
Either use floor() or int(), or subtract one.
|
| Show full article (1.50Kb) |
| no comments |
|
  |
Author: Ron FordRon Ford Date: Sep 16, 2008 23:52
On Tue, 16 Sep 2008 20:45:39 -0700 (PDT), e p chandler posted:
> No. It looks like you have an off by one bug. Consider this test
> program:
I think I have it, as shown by adding up the outcomes:
summa = sum (k)
print *, "sum =", summa
n= 1
clock= 311964079
seed= 311964079
1 0.809851
2 0.268532
3 0.787813
4 0.293169
C =...
|
| Show full article (1.70Kb) |
| no comments |
|
  |
Author: Ron FordRon Ford Date: Sep 17, 2008 00:25
On Tue, 16 Sep 2008 23:27:15 -0800, glen herrmannsfeldt posted:
> Ron Ford wrote:
>
>
>> I *do* have off by one bug, but I think we're both wrong at this point, as
>> your classical way to roll a die has uneven outcomes.
>
> All will have slightly uneven outcome...
|
| Show full article (3.76Kb) |
| no comments |
|
  |
Author: glen herrmannsfeldtglen herrmannsfeldt Date: Sep 17, 2008 02:25
Ron Ford wrote:
(snip, I wrote)
>>As far as the original question, I believe it is
>>one fourth, not one eighth.
(snip)
> How did we miss half of the cases?
(snip)
> %%- INT_MAX is 7. UB is invoked when the values are:
> %%-
> %%- 7 and any of -1, -2, -3, -4, -5, -6, -7, -8
> %%- 6 and any of -2, -3, -4, -5, -6, -7, -8
> %%- 5 and any of -3, -4, -5, -6, -7, -8
> %%- 4 and any of -4, -5, -6, -7, -8
> %%- 3 and any of -5, -6, -7, -8
> %%- 2 and any of -6, -7, -8
> %%- 1 and any of -7, -8
> %%- 0 and -8
The other half are negative numbers minus positive numbers,
with the result below -8.
|
| Show full article (0.62Kb) |
| no comments |
|
  |
Author: e p chandlere p chandler Date: Sep 17, 2008 06:38
On Sep 17, 2:52Â am, Ron Ford wrote:
> Why do I believe that
> b = min(max(1,ceiling(harvest*sides)),sides)
> is better than
> Â b = -i_max + int( harvest * i_tot )
That's a matter of "taste". :-). For each of us our choice illustrates
our own thought process.
- e
|
| |
| no comments |
|
  |
Author: Ron FordRon Ford Date: Sep 18, 2008 16:02
On Wed, 17 Sep 2008 06:38:36 -0700 (PDT), e p chandler posted:
> On Sep 17, 2:52Â am, Ron Ford wrote:
>
>> Why do I believe that
>> b = min(max(1,ceiling(harvest*sides)),sides)
>> is better than
>> Â b = -i_max + int( harvest * i_tot )
>
> That's a matter of "taste". :-). For each of us our choice illustrates
> our own thought process.
I think your taste is more legible than mine. Unsurprisingly, Glen is
right about the ultimate calculation here:
|
| Show full article (2.99Kb) |
| no comments |
|
  |
|
|
  |
Author: Ron FordRon Ford Date: Sep 18, 2008 16:33
On Wed, 17 Sep 2008 02:26:26 -0800, glen herrmannsfeldt posted:
> Ron Ford wrote:
> (snip, I wrote)
|
| Show full article (1.32Kb) |
| no comments |
|
|
|
|