Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

ROUND and INT without decimal point and "0" ?

 
Post new topic   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
Greece2001

Bascom Member



Joined: 06 Apr 2009
Posts: 57

greece.gif
PostPosted: Tue Jun 20, 2017 8:53 am    Post subject: ROUND and INT without decimal point and "0" ? Reply with quote

Hi,
according to the Help-File, ROUND should work like this:
Round(2.3) = 2 , Round(2.Cool = 3
Round(-2.3) = -2 , Round(-2.Cool = -3
-> no decimal point, no trailing zeros Very Happy
INT is explained as : The integer is the left side before the decimal point.

Unfortunately, ROUND(2.3) produces 2.0, ROUND(2.Cool produces 3.0

The sample
Code:
$Regfile="2313def.dat"
$Crystal=8000000
$hwstack=32
$swstack=8
$framesize=50
'-------------------------------------------------------------------------------
'                            ROUND_FIX_INT.BAS
'-------------------------------------------------------------------------------
Dim S As Single , Z As Single
For S = -10 To 10 Step 0.5
  Print S ; Spc(3) ; Round(s) ; Spc(3) ; Fix(s) ; Spc(3) ; Int(s)
Next
End

does this as well.

Any way to remove the decimal point and the trailing zero so that ROUND will work as expected ?

Thanks already for the replies !


(BASCOM-AVR version : 2.0.7.9 , Latest : 2.0.7.8 )
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4974
Location: Holland

blank.gif
PostPosted: Tue Jun 20, 2017 8:59 am    Post subject: Reply with quote

The .0 is left away for clarity.
Since the data type does not change, you always will have the .0 when showing the value as a string.(or when printing).
The functions them self act as they should.

Maybe you can change to long ?
SomeLong = SomeDouble : print SomeLong

of course this will only help when the value fits.

Another option is to convert to a string and strip the .0 with the LEFT() function.

_________________
Mark
Back to top
View user's profile Visit poster's website
Greece2001

Bascom Member



Joined: 06 Apr 2009
Posts: 57

greece.gif
PostPosted: Tue Jun 20, 2017 9:20 am    Post subject: Reply with quote

Hi Mark,

albertsm wrote:
The .0 is left away for clarity.
Since the data type does not change, you always will have the .0 when showing the value as a string.(or when printing).
The functions them self act as they should.

Maybe you can change to long ?
SomeLong = SomeDouble : print SomeLong

of course this will only help when the value fits.

Another option is to convert to a string and strip the .0 with the LEFT() function.


Well well, that explains a lot Shocked
Checked and tried for several hours 'cause of this wrong description Sad
And the .0 is a redundant and needless information as it will always stay at .0 .
Any chance that the ROUND-routine can be fixed ? If a user wants the .0, maybe ROUND can be extended so one could select
the fraction or if .0 is displayed or not.

BTW, INT has the same problem. It is supposed to return the INTEGER-Part. The .0 is not part of the INTEGER.

No, can't change to Long, the source has about 3500 lines of code.
Just wanted to add an option to log several values via the UART to display them in several different ways.
Converting to STR and then stripping away the .0 would work. Have to log about 8 to 10 values, so the conversion and stripping will bloat the program unnecessarily.

Regards,
Greece2001
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4974
Location: Holland

blank.gif
PostPosted: Tue Jun 20, 2017 9:31 am    Post subject: Reply with quote

When you assign 1 to an FP you also assign 1.0. When there is no fraction specified it is .0 Just when you have an article of 1 euro, it is 1,00
so there is nothing wrong with the description. you use a floating point variable. There is a comma and fraction implicitly. The functions take care of the fraction or integer part of the variable, but the variable remains a FP variable.

_________________
Mark
Back to top
View user's profile Visit poster's website
Greece2001

Bascom Member



Joined: 06 Apr 2009
Posts: 57

greece.gif
PostPosted: Tue Jun 20, 2017 10:12 am    Post subject: Reply with quote

albertsm wrote:
When you assign 1 to an FP you also assign 1.0. When there is no fraction specified it is .0 Just when you have an article of 1 euro, it is 1,00
so there is nothing wrong with the description. you use a floating point variable. There is a comma and fraction implicitly. The functions take care of the fraction or integer part of the variable, but the variable remains a FP variable.


Of course, if I assign 1 to a FP it is 1.0.
1 with integer and fraction is 1.00
1.00 without fraction is 1, not 1.0

This is not about assigning a value or the contents of the FP but about the way the value is displayed after ROUND and INT.
If the Help-File says Round(2.8) will produce a result of 3 but in reality the result is 3.0, this is a major difference.


Best regards,
Greece2001
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4974
Location: Holland

blank.gif
PostPosted: Tue Jun 20, 2017 12:30 pm    Post subject: Reply with quote

The functions operate on the variable, they do not operate on how you display these values.
You best read the topic about FP in the help. Maybe that will make things more clear.
Integer you mention have nothing to do with it. Using integers is a different concept. But as i mentioned you can assign a long from a double to convert to an integer number.
Notice that INT() function also works on FP and does not make an integer out of an FP variable.
An FP has an integer part (before the comma) and a fractional part, after the comma. Assigning 1.0 or 1 does the same : the integer part is set, and the fractional part as well. But all this time the variable remains a FP number.

_________________
Mark
Back to top
View user's profile Visit poster's website
Greece2001

Bascom Member



Joined: 06 Apr 2009
Posts: 57

greece.gif
PostPosted: Tue Jun 20, 2017 1:35 pm    Post subject: Reply with quote

Hello Mark,
I do understand FP, integers etc. and I do have many years of programming "on my back".
But the way BASCOM handles ROUND() and INT() is confusing.

Quote:
The functions operate on the variable, they do not operate on how you display these values

Certainly, something like
Print round(SingleVar)
will only influence the way it is displayed and not the contents of 'SingleVar' ?!

Quote:
An FP has an integer part (before the comma) and a fractional part, after the comma. Assigning 1.0 or 1 does the same : the integer part is set, and the fractional part as well. But all this time the variable remains a FP number.

Of course, that is what I am saying all the time. But maybe my english is too bad ?
And of course INT() does not transform a FP into an integer, it is supposed to "extract" the integer part from the FP.

In other, simpler words, what I expected was:
Code:
Dim S As Single
S = 1234.8
print round(S); Spc(3) ; int(S)


to print a result like this:
1235 1234
and not
1235.0 1234.0
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4974
Location: Holland

blank.gif
PostPosted: Tue Jun 20, 2017 2:13 pm    Post subject: Reply with quote

the fact that you add a round() function does not mean that all of a sudden, the number is treated different. The result of round() remains an FP and thus when printing it, it will print the fraction as well.
If for zero fraction no .0 was shown i am sure it would also raise questions.

_________________
Mark
Back to top
View user's profile Visit poster's website
Greece2001

Bascom Member



Joined: 06 Apr 2009
Posts: 57

greece.gif
PostPosted: Wed Jun 21, 2017 7:22 am    Post subject: Reply with quote

albertsm wrote:
the fact that you add a round() function does not mean that all of a sudden, the number is treated different. The result of round() remains an FP and thus when printing it, it will print the fraction as well.
If for zero fraction no .0 was shown i am sure it would also raise questions.


Quote:
The result of round() remains an FP and thus when printing it, it will print the fraction as well


As the fraction is always 0 when ROUND() is used, it is needless.
That the original FP is not changed is an implicitness.

Having a fraction after using INT() is a contradiction in itself.

Let's finish this discussion. And let's agree that we will not agree on this subject.

Best regards,
Greece2001
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1801

blank.gif
PostPosted: Wed Jun 21, 2017 8:52 am    Post subject: Reply with quote

Greece2001 wrote:
Let's finish this discussion. And let's agree that we will not agree on this subject.
Let's asses that you have not much clue of what you're talking about.

Quote:
Having a fraction after using INT() is a contradiction in itself.
Having a decimal point and a fraction displayed, even the fraction is zero, is no contradiction.

What Mark tries to make you understand is: Print is the command of displaying the float and Round/Int are the commands to alter it.
In a program, not even if Round/Int is on one line with Print, the commands neither have to, nor do know from each other.
Actually, commands in one line are internally split up in several commands executed one to another.

So the Print calls the function Round(), the returned value is a float and thus the float is displayed according it's type with a trailing fraction, even the fraction is zero.
That's a rule of logic and of the compiler, take it or leave it.

In case you need to display it differently, there are commands available, it is called "writing code".
And in case you think the compiler has to handle this, it would mean that the Print-command has to check in case of floats whether there is a fraction of zero, and if it is, omit the trailing dot and zero. Besides other users would complain, it would be a illogicalness in itself.

Why should the compiler do that?
Because you don't understand, or can't do the few lines of code to format the output according your desire?

How about a different hobby?
Back to top
View user's profile
Greece2001

Bascom Member



Joined: 06 Apr 2009
Posts: 57

greece.gif
PostPosted: Wed Jun 21, 2017 9:19 am    Post subject: Reply with quote

MWS wrote:
Let's asses that you have not much clue of what you're talking about.

Really ? But you have all the knowledge in the world Wink

Quote:

Why should the compiler do that?
Because you don't understand, or can't do the few lines of code to format the output according your desire?

I already did that (writing the code to format the output) when this topic was started.

Quote:

How about a different hobby?

Well, until now, this was a normal discussion.
Why do you think you have to insult me in this unpolite way ?

Quote:

How about a different hobby?

No, but may be a different forum ?

.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1801

blank.gif
PostPosted: Wed Jun 21, 2017 1:33 pm    Post subject: Reply with quote

Greece2001 wrote:
But you have all the knowledge in the world Wink
Surely not. But I know what such a tiny machine and the compiler can do.
Quote:
Well, until now, this was a normal discussion.
If you consider talking to a concrete wall to be a normal discussion...
Quote:
Why do you think you have to insult me in this unpolite way ?
It was an advice you may think about, as stubbornness is simply not beneficial for this kind of topic, while other hobbies may benefit from.
Back to top
View user's profile
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Wed Jun 21, 2017 4:52 pm    Post subject: Icecream.. Reply with quote

Perhaps we all should take an icecream to cool down. So much trouble in the world....

Ben Zijlstra
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4974
Location: Holland

blank.gif
PostPosted: Wed Jun 21, 2017 8:40 pm    Post subject: Reply with quote

Quote:
As the fraction is always 0 when ROUND() is used, it is needless.
That the original FP is not changed is an implicitness.

Having a fraction after using INT() is a contradiction in itself.

Let's finish this discussion. And let's agree that we will not agree on this subject.


I tried to explain it a few times but it seems a waste of my time.
This is where my support ends.

_________________
Mark
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum