Forum - MCS Electronics

Author Message
Greece2001

Joined: 06 Apr 2009
Posts: 57

Posted: Tue Jun 20, 2017 8:53 am    Post subject: ROUND and INT without decimal point and "0" ?

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

Unfortunately, ROUND(2.3) produces 2.0, ROUND(2. 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 )
albertsm

Joined: 09 Apr 2004
Posts: 5205
Location: Holland

 Posted: Tue Jun 20, 2017 8:59 am    Post subject: 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
Greece2001

Joined: 06 Apr 2009
Posts: 57

Posted: Tue Jun 20, 2017 9:20 am    Post subject:

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
Checked and tried for several hours 'cause of this wrong description
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
albertsm

Joined: 09 Apr 2004
Posts: 5205
Location: Holland

 Posted: Tue Jun 20, 2017 9:31 am    Post subject: 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
Greece2001

Joined: 06 Apr 2009
Posts: 57

Posted: Tue Jun 20, 2017 10:12 am    Post subject:

 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
albertsm

Joined: 09 Apr 2004
Posts: 5205
Location: Holland

 Posted: Tue Jun 20, 2017 12:30 pm    Post subject: 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
Greece2001

Joined: 06 Apr 2009
Posts: 57

Posted: Tue Jun 20, 2017 1:35 pm    Post subject:

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
albertsm

Joined: 09 Apr 2004
Posts: 5205
Location: Holland

 Posted: Tue Jun 20, 2017 2:13 pm    Post subject: 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
Greece2001

Joined: 06 Apr 2009
Posts: 57

Posted: Wed Jun 21, 2017 7:22 am    Post subject:

 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
MWS

Joined: 22 Aug 2009
Posts: 1920

Posted: Wed Jun 21, 2017 8:52 am    Post subject:

 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?

Greece2001

Joined: 06 Apr 2009
Posts: 57

Posted: Wed Jun 21, 2017 9:19 am    Post subject:

 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

 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 ?

.
MWS

Joined: 22 Aug 2009
Posts: 1920

Posted: Wed Jun 21, 2017 1:33 pm    Post subject:

 Greece2001 wrote: But you have all the knowledge in the world
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.
bzijlstra

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

 Posted: Wed Jun 21, 2017 4:52 pm    Post subject: Icecream.. Perhaps we all should take an icecream to cool down. So much trouble in the world.... Ben Zijlstra
albertsm

Joined: 09 Apr 2004
Posts: 5205
Location: Holland

Posted: Wed Jun 21, 2017 8:40 pm    Post subject:

 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
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First
 All times are GMT + 1 Hour Page 1 of 1

 Jump to: Select a forum BASCOM AVR/8051----------------BASCOM-AVRBASCOM-8051BASCOM-ARDUINOShare your working BASCOM-8051 code hereShare your working BASCOM-AVR code hereBASCOM BETA-SLA BASCOM Related----------------EASY TCP/IPAVR-DOSAR7212KokkeKat FAT-free SD card libBASCOM Project Blog Other Stuff----------------VariousPCB'sRoboticsNew WebSiteAnnouncementsAVR Archive----------------BASCOM-AVR ArchiveBASCOM-8051 ArchiveBASCOM-AVR Unsupported versionsEasy TCP/IP ArchiveBASCOM-EDB
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