View previous topic :: View next topic |
Author |
Message |
Walter
Joined: 14 Nov 2007 Posts: 18
|
Posted: Sun Feb 24, 2019 1:18 am Post subject: Bascom makes an unexpected rounding |
|
|
By chance I discovered that Bascom is doing an unexpected rounding by itself in special cases, but not always.
In the following example single and double have different behavior.
Single1=123.6: Word1=Single1 'truncate, no rounding
Double1=123.6: Long1=Double1 'unexpected rounding
I did not expect this. To my knowledge, each programming language will truncate the fraction in this cases.
This is not described in the manual. It should be under Casting in Language Fundamentals. Without test, I would have rounded twice in my application and thus had a wrong result.
I think that is a mistake, or have I overlooked something?
(BASCOM-AVR version : 2.0.8.1 ) |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Sun Feb 24, 2019 9:42 am Post subject: Re: Bascom makes an unexpected rounding |
|
|
Walter wrote: | I did not expect this. |
And what you're doing now, starting to cry?
Quote: | To my knowledge, each programming language will truncate the fraction in this cases. |
You know each programming language? Wow, you must be a pretty clever guy.
In case you get even more smart, you will use
Code: | Double1 = Int(Double1) |
before assigning to an integer, as then you have an exactly defined behavior and you don't fall prey to your expectation. |
|
Back to top |
|
|
Walter
Joined: 14 Nov 2007 Posts: 18
|
Posted: Sun Feb 24, 2019 10:39 am Post subject: |
|
|
Bascom does not allow different data types in an arithmetic operation. That's why I have to assign the types of variables before, like Long1=Double1. That's what I usally do. According to your statement, I can not expect a definite behavior. |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Sun Feb 24, 2019 11:41 am Post subject: |
|
|
Walter wrote: | According to your statement, I can not expect a definite behavior. |
According my statement you can not count on YOUR expectation.
By using the suggested command you can make sure realworld matches expectation. |
|
Back to top |
|
|
|