robinbloke: (Default)
[personal profile] robinbloke
Spot the error, win a pint/drink thereof... and no, I didn't write this



int SimpleRound ( double high )
{

int low = (int) high;
double diff;
int result;

diff = high - low;
if (diff>=0)
result = ( diff >= (double)0.5000000 ) ? (low + 1) : low;
else
result = ( diff >= (double)-0.5000000 ) ? (low - 1) : low;
return (result);
}

Date: 2003-03-12 06:36 am (UTC)
From: [identity profile] borusa.livejournal.com
result = ( diff >= (double)-0.5000000 ) ? (low - 1) : low;

isn't right

Should be

result = ( diff < (double)-0.5000000 ) ? (low - 1) : low;

That right?

While I'm not a programmer...

Date: 2003-03-12 06:41 am (UTC)
From: [identity profile] lanfykins.livejournal.com
result = ( diff >= (double)0.5000000 ) ? (low + 1) : low;

a) is that a multiplication
b) should it be?

Date: 2003-03-12 06:54 am (UTC)
From: [identity profile] robinbloke.livejournal.com
Indeed so, for the first correct answer; you win a beverage of your choice :)

Date: 2003-03-12 06:54 am (UTC)
From: [identity profile] crocodilewings.livejournal.com
Floating point error: Domain.

Re: While I'm not a programmer...

Date: 2003-03-12 06:55 am (UTC)
From: [identity profile] robinbloke.livejournal.com
Nope it's a macro C style if...then...else statement, which I hate.
RBS got the correct answer :)

Date: 2003-03-12 06:56 am (UTC)
From: [identity profile] robinbloke.livejournal.com
I'm afraid that's not the answer on my card.

Date: 2003-03-12 06:57 am (UTC)
From: [identity profile] crocodilewings.livejournal.com
It's the answer on the DOS prompt!

Date: 2003-03-12 07:25 am (UTC)
From: [identity profile] borusa.livejournal.com
It's a really dim way to do it as well.

int SimpleRound ( double high )
{
double temphigh;
temphigh += (double)0.5;
return (int)temphigh;
}

is so much more elegant.
And even that is poor, really, as it relies on the cast working as you expect. There's almost certainly a library function to do this anyway, and that should be used.

Date: 2003-03-12 07:25 am (UTC)
From: [identity profile] borusa.livejournal.com
correction

double temphigh=high;

Stupid non-edity comments

Date: 2003-03-12 07:27 am (UTC)
From: [identity profile] davefish.livejournal.com
They have forgotten all the line numbers.

Runs of to hide

Date: 2003-03-12 07:36 am (UTC)
From: [identity profile] robinbloke.livejournal.com
Well the fun with this is; the above routine is reading in values from a text file from program B; The program I'm trying to fix (A) is writing these values; meaning I have to frig the values in order that it reads them correctly into the B, since I can't modify the code for it.
Huzah.

Date: 2003-03-12 07:36 am (UTC)
From: [identity profile] robinbloke.livejournal.com
Ahhh BBC Basic... I miss it so :(

Date: 2003-03-12 07:37 am (UTC)
From: [identity profile] robinbloke.livejournal.com
Well it's a code fragment, rather than a working program...

Date: 2003-03-12 07:55 am (UTC)
From: [identity profile] crocodilewings.livejournal.com
That's why I set it to main before compiling.

Date: 2003-03-12 07:56 am (UTC)
From: [identity profile] davywavy.livejournal.com
Oh, riiiiight

Hold competitions that I can't win booze from you in, I see :p

Date: 2003-03-12 07:57 am (UTC)
From: [identity profile] robinbloke.livejournal.com
Ahh well, you'd need to call it from main rather than set it to main; that would do the trick, probably.

Date: 2003-03-12 07:58 am (UTC)
From: [identity profile] robinbloke.livejournal.com
Not my fault you can't code...

Date: 2003-03-12 02:41 pm (UTC)
From: [identity profile] borange.livejournal.com
mmm COMAL ...(iirc its what we were taught at school - sez the man given 36 hours to re-write his entire
O'grade project before final submission due to him having written it in BASIC and no teacher decided to tell me before then - damn them - still first successful oevr-nighter of code writing'ness how its stood me in good stead since then :O)

Profile

robinbloke: (Default)
robinbloke

January 2016

S M T W T F S
     12
3456789
10111213141516
17181920212223
24 252627282930
31      

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Mar. 20th, 2026 12:54 pm
Powered by Dreamwidth Studios