BASIC INT rounding issue
David J. Ruck (33) 1587 posts |
This is all very simple to work around, when converting to integer, use |
Clive Semmens (2335) 3274 posts |
Binary floating point can represent, with perfect accuracy, any fraction of the form
Decimal floating point can represent, with perfect accuracy, any fraction of the form
(I leave it as a task for the reader to generalise this to base X.)
Indeed, if what you want of INT is to round to nearest, rather than round down. The inconsistencies will then occur around anything of the form |
Paul Sprangers (346) 516 posts |
It’s a matter of how many divisors base X has. The decimal system has 4 (1-2-5-10), the duodecimal system has 6 and the Babylonian sexagesimal system even has 12! The more divisors, the more fractions can be represented with perfect accuracy. But how would we know how many divisors base X has? |
Clive Semmens (2335) 3274 posts |
Exactly, Paul – but worth noting that the Babylonians with their base 60 do no better on this than they would with base 30. The extra factor of 2 would make some fractions shorter, but wouldn’t make any difference to what could be accurately represented. |
Jean-Michel BRUCK (3009) 334 posts |
The sexagesimal system is important for time display, the 12 is necessary, it is the position of the numbers on a watch. |
Clive Semmens (2335) 3274 posts |
Indeed, but that’s not relevant to the precise representation of fractional quantities! :-) The 24 hour clock might encourage folks to step up yet further, to base 120…gee thanks, no thanks. I think I like to do sexagesimal as decimal coded sexagesimal (which is what we actually do with time) and the 12 or 24 hours of the clock as decimal coded, too… |
Jon Abbott (1421) 2640 posts |
Getting back to the OP… Is PRINT using a different rounding method to INT? |
Clive Semmens (2335) 3274 posts |
Almost certainly. INT (correctly) uses round down; everything else ought to be, and probably is, using round to nearest. There’s no way to avoid issues of this kind, when you work to different precisions. David Ruck’s method ensures that INT rounds to nearest, not down, but in floating point you’ll still get strings of 9s sometimes, and strings of zeroes with the odd different digit at the end other times – different depending what precision you work to. You can then fix those by rounding to nearest with less precision, of course. |
Colin Ferris (399) 1798 posts |
Sounds a bit like the PostOffice bean counters :-/ |
Clive Semmens (2335) 3274 posts |
They didn’t have the excuse that the end user was trying to use floating point…never mind floating point in two different systems with different levels of precision… |
Rick Murray (539) 13753 posts |
I doubt people that write this đź’© would even have noticed, or, come to think of it, understand any part of this discussion. Public Function ReverseSign(d) If d < 0 Then d = Abs(d) Else d = d - (d * 2) End If ReverseSign = d End Function Yes, that’s actual code to perform If this is what Fujitsu considers acceptable code to put into something costing many tens of millions, I feel that all of their code in other government contracts needs to be audited. |
Clive Semmens (2335) 3274 posts |
I’d have thought, dumped and restarted from scratch, by someone competent would have been better… |
David J. Ruck (33) 1587 posts |
I’ve seen lots of code like that from the large Indian outsourcing companies, and it looks like someone of their sweatshop coders managed to escape to ICL’s old offices. Functions may coincidentally give the intended result, but its a huge bloated mess that will have been cut and pasted into dozens of other places, making it completely unmaintainable. It really pissed me off the other day when I read someone saying software engineers may not be replaced by AI, but coding will as it’s just “rote” conversion from the requirements. Well what do you think AI is trained on and more likely to regurgitate, is it the a small set of well coded efficient and maintainable source, or the gigantic torrent of low quality shit coming from the subcontinent? |
Colin Ferris (399) 1798 posts |
I wonder how much of government coding is like this? |
David J. Ruck (33) 1587 posts |
All of it. |
Rick Murray (539) 13753 posts |
👆 And the next time, the government will offer tens of millions and the usual shitshows will apply and your tax money will be spaffed away on that (plus “consultants” paid a grand per day) instead of… #include <woefully long list of things more important to normal people: roads that function, schools that function, hospitals that function, social services and support that functions, policing that functions……..> |
Rick Murray (539) 13753 posts |
The other day, Druck, wasn’t the guy running nVidia that opened his mouth, was it? I mentioned in a blog article the danger of requiring a written specification to be followed exactly, when the person writing the specification doesn’t understand the machine – leading to nonsense like using a fixed point value as a loop counter (and translating from that to something to bung into a register, and back, over and bloody over because the spec describes it like this … we’re counting to 80 (or something like that) FFS, just bung it into a register and be done with it). It’s the job of a programmer to translate the spec into functional code that does what the spec says should happen. If “they” (whoever follows this advice) wants a blind idiot translation, then by all means ask an AI to help. Just kindly don’t ask us meatsacks to rescue them from the mess that they’re going to end up in, unless of course they are offering silly money. Wanna pay me a grand a day, I’ll help ‘em sort out whatever AI generated mess they have on their hands (most likely by binning it all and doing what they should have done correctly had they not been obsessing over AI because it’s cheaper, not because it’s better). |
Clive Semmens (2335) 3274 posts |
Red rag to a bull there! You pay peanuts, you get monkeys…yes, of course India has plenty of monkeys. They’ve also got plenty of well educated, competent workers – but the monkeys are cheaper. Blame the folk who choose to employ the monkeys… (of course the well educated competent workers will very likely end up migrating to Europe or the USA, I knew some of them at ARM, but that’s another issue…) |
Colin Ferris (399) 1798 posts |
It takes something – to design a system that thinks it ok to fly a plane into the ground – with no quick override. |
David J. Ruck (33) 1587 posts |
They have got some great people, very knowledgeable, easy to get on with and can code, I’d have loved to have the guy who was supposed to be our on-site lead working as part of our team. He really understood the problem and did great work during the proof of concept phase which cliched the deal, but as soon as we signed on the dotted line, we find he’s gone back to to his family in India, and what we were given in return is utterly useless. They promise the original guy will be in charge of the offshore team, except he isn’t, he’s off deceiving the next victim (knowingly or unknowingly). The offshore team then produces the crap, the new on-site guy seems to no more be able to communicate with them than us, his only purpose is tell his boss about our complaints, who then phones us to say that whatever we want (little things such as working code) isn’t on the all powerful spreadsheet of agreed work, and wont be done without renegotiating the contract at huge expense. Suffice to say we jettisoned this shower of proverbial after 3 months (with the prior 2 months wasted on tendering and POC), with not a single useable piece of code to show for the tens of thousands spent – at least the company couldn’t say that I hadn’t told them exactly how it would turn out from the very beginning. Of course with AI it’s going to be soooo different; the good guy will still act as the hook, the useless guy will still do nothing except phone his boss, the boss will claim the request isn’t on the spreadsheet, but the offshore team will be unemployed and the bad code will be spewed out in even greater quantities by the hallucinating LLM. |
Simon Willcocks (1499) 506 posts |
Been there, done that. Some really good people, but one who had to ask “where does it return to?” |
Clive Semmens (2335) 3274 posts |
There are monkeys and good people everywhere. India is much bigger, and has far more of both. Offshore anything, and to a considerable extent you lose control over which you get. And monkeys are cheaper. |
Rick Murray (539) 13753 posts |
That scam works upon most people being to honest to even think about putting a bait-and-switch clause into contracts.
And that’s how it works. Those that understand pay up for nothing, those that don’t understand pay up more for crap. But, then, when has offshoring ever been a good idea. In return for “cheaper labour” you’re putting physical barriers (distance, timezones, maybe even language) in between the management and those doing the work, plus all of those employees will have their contracts (and loyalty) to the company that employs them, not to you. So… apart from making the next quarter’s profits look better (until the poop hits the air blower), it doesn’t seem…like a competent decision. |
Peter Howkins (211) 235 posts |
Please all, stop dragging threads off topic. Or at least when you do, have the decency to move to Aldershot. |
Graeme (8815) 103 posts |
This Fujitsu/Post Office code is actually quite interesting. It is not the same as d=-d for a variety of reasons that could depend on the language and assuming d is floating point. Take this line which reverses the sign: d = d - (d * 2) This reverses the sign for numbers with values. For zero and negative zero (yes, negative zero is a thing in floating point) it looks like it changes it into positive zero for both cases, making the negative zero go away. Perhaps this is a poor attempt at a bug fix. Interestingly I have tried the following in BASICVFP and BASIC64 to see what happens: d = 0 d=-d PRINT d After this, x is negative zero. That last PRINT x will print -0 while BASIC (standard) and BASICFPA will print 0. Then d=d-(d*2) will always result in zero. At least in these BASIC versions -0 does equal +0 but other languages may see a difference between the two and this could be a (poor) fix for that. |