April 30, 2024, 04:12:08 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Percent symbol

Started by Andy, March 11, 2016, 01:09:03 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

I was looking at a bit of code that was posted a good while ago, but it has me confusded (an easy thing to accomplish!).

What is the percentage symbol actually doing here... as I can't understand the return values.

I'm using a value of say 20 (decimal), and want the answer in binary (base 2).

I get the following print values....

1 20 2
1 10 2
2 5 2
1 2 2
2 1 2

Whats happening with the 20, 10, 5, 2 and 1??

Confused.
Andy.
???


openconsole
print
print
print " ",ToBase(val("20"),2)
print
print
waitcon
end

Sub ToBase(intValue As Int,Base As Int),String
   Dim result As String
   Dim i As Int
   result = ""
   If (base >= 2) & (base <= 36)
      While intValue > 0
         i = (intValue % base) + 1

         print " values ",i,intvalue,base

         intValue = intValue / base
         result = Mid$("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",i,1)+result
      EndWhile
   EndIf
   
   print
   Return result
EndSub



Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

LarryMc

Those print outs are not the answer, they are just the troubleshooting printout.
The answer is 10100.

What is confusing you? The % sign is the MOD operator BTW.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

LarryMc

I tell you what;
I'll talk you through the whole example going through the entire loop and you'll see how easy it is for any base.

It'll all be in my next post.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

LarryMc

Since we are dealing in one base at a time we don't need the if statement about our base and we're really just dealing with this while loop:
we're wanting to convert 20 base10 to base2
so we pass inValve=20  and base=2 to the subroutine
   result=""
      While intValue > 0         :1st time here its 20
         i = (intValue % base) + 1      :intValue[20] %base[2] which is 20/2 with no remainder=0+1 so i=1

         print " values ",i,intvalue,base   :1,20,2
                     :what we just solved for was least significant bit of answer
         intValue = intValue / base       :20/2=10
   since i=1 this pass
         result = Mid$("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",i,1)+result
         result="0"
   goto next pass -see below
      EndWhile

      While intValue > 0         :2nd time here its 10
         i = (intValue % base) + 1      :intValue[10] %base[2] which is 10/2 with no remainder=0+1 so i=1

         print " values ",i,intvalue,base   :1,10,2
                     :what we just solved for was 2nd least significant bit of answer
         intValue = intValue / base       :10/2=5
   since i=1 this pass
         result = Mid$("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",i,1)+result
         result="00"
   goto next pass -see below
      EndWhile

      While intValue > 0         :3rd time here its 5
         i = (intValue % base) + 1      :intValue[5] %base[2] which is 5/2 with remainder=1+1 so i=2

         print " values ",i,intvalue,base   :2,5,2
                     :what we just solved for was 3d least significant bit of answer
         intValue = intValue / base       :5/2=2
   since i=2 this pass
         result = Mid$("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",i,1)+result
         result="100"
   goto next pass -see below
      EndWhile

      While intValue > 0         :4th time here its 2
         i = (intValue % base) + 1      :intValue[2] %base[2] which is 2/2 with remainder=0+1 so i=1

         print " values ",i,intvalue,base   :1,1,2
                     :what we just solved for was 4th least significant bit of answer
         intValue = intValue / base       :2/2=1
   since i=1 this pass
         result = Mid$("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",i,1)+result
         result="0100"
   goto next pass -see below
      EndWhile

      While intValue > 0         :5th time here its 1
         i = (intValue % base) + 1      :intValue[1] %base[2] which is 1/2 with remainder=1+1 so i=2

         print " values ",i,intvalue,base   :2,1,2
                     :what we just solved for was 4th least significant bit of answer
         intValue = intValue / base       :1/2=0
   since i=2 this pass
         result = Mid$("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",i,1)+result
         result="10100"
   since intValue is 0 exit loop -see below
      EndWhile
     20 base 10 = 10100 base 2

That should do it if I didn't make a typo
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Andy

Larry,

Thanks for the detailed and concise answer to that!

So essentially % (mod) is doing this...

11 % 3

3 goes into 11   
3 times (giving 9) - any more and it would be 12 which is greater than 11.

So we take our number (11) and subtract the "sum up" of multiples of 3 (9)
11 - 9 = 2

So 2 is the answer.
( 11 % 3 = 2).

If the mod number - here 4 goes exactly into the original number we get 0 (zero).

12 % 4 = 0

The code is very clever!

Thanks for the Maths lesson Larry!!

Andy.
:)


Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

LarryMc

LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Bill-Bo

March 11, 2016, 06:28:07 AM #6 Last Edit: March 11, 2016, 06:35:52 AM by Bill-Bo
andy and LarryMc,

I sure do like this program. Plus, LarryMc, your detailed explanation. I have taken the liberty of modifying it to convert decimal numbers to the base 2 to 36. But, for the life of me, I forget the code to give you the chose of running the program again, or not. Could PLEASE help out on that?

Here is what I have done:

openconsole
dim Dec$ as string
dim base as int
print "Convert a decimal number to a base from 2 to 36."
print
input "Enter the decimal number: ", Dec$
input "Enter the base: ", base
print " ",ToBase(val(Dec$),base)
print
print
waitcon
end

Sub ToBase(intValue As Int,Base As Int),String
   Dim result As String
   Dim i As Int
   result = ""
   If (base >= 2) & (base <= 36)
      While intValue > 0
         i = (intValue % base) + 1

         print " values ",i,intvalue,base

         intValue = intValue / base
         result = Mid$("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",i,1)+result
      EndWhile
   EndIf
   
   print
   Return result
EndSub



On an aside. In my programming, I have used binary, decimal, and hexadecimal numbers. And in the old days, even octal. Even with my interest in math, I have not had call to use other than the bases 2, 8, 10, and 16. So, looking at the program, I wondered, andy, why in the MID$ you have 0 thru Z, instead of 0 thru F. After my modification, I played around with some different numbers. I came across decimal 60 to base 35, with a result of 1P. Now I know why.

BTW Shouldn't there be a CLOSECONSOLE? Also, for some reason, when I post the code in the codebox, the keywords are in lower case?

I sure do like it, and I will use it a lot. Just hope, Larry, that you can help me out.

Thanks, again, for a very interesting program,

Bill

Andy

March 11, 2016, 07:19:56 AM #7 Last Edit: March 11, 2016, 07:21:28 AM by andy1966
Bill,

Thanks for the code, I will have a look at it as soon as I can.

I did not write the code posted, my one brain cell won't let me write something so good.

I was working on converting ascii to hex, and then hex to binary, that's why I was looking at the code I posted hence my question on the percent operator..

Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

LarryMc

openconsole
dim Dec$ as string
dim base as int
DEF key$:STRING
label start
print "Convert a decimal number to a base from 2 to 36."
print
input "Enter the decimal number: ", Dec$
input "Enter the base: ", base
print " ",ToBase(val(Dec$),base)
print
print

PRINT "Press <Q> to end program"
PRINT "Press any other keys to re-run"
DO
    key$ = INKEY$
UNTIL key$  <> ""
If key$="q" or key$="Q"
CLOSECONSOLE
end
else
cls
goto start
endif


Sub ToBase(intValue As Int,Base As Int),String
   Dim result As String
   Dim i As Int
   result = ""
   If (base >= 2) & (base <= 36)
      While intValue > 0
         i = (intValue % base) + 1

        ' print " values ",i,intvalue,base

         intValue = intValue / base
         result = Mid$("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",i,1)+result
      EndWhile
   EndIf
   
   print
   Return result
EndSub

LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Bill-Bo

LarryMc,

Thanks a lot. Works great.

Bill

Andy

Thought I would try this with large decimal numbers, so to do it I have written in a few more functions
to my StringMap library.

I've included the sub routine (commented out) so you can see the difference.

This now also gives me the possibility to add more maths functions and to work with different base numbers.

Have a look, you can give a large number such as 123456789012345 (decimal) etc...

You will see a delay of a few seconds, but remember it does work with large numbers.

Unzip to a folder
Copy the .lib folder to your IWBDev / libs folder
Compile and run

Have fun - please let me know if it works for you or you have a problem.

Thanks as always,
Andy.
:)
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

Bill-Bo

March 13, 2016, 06:11:09 PM #11 Last Edit: March 13, 2016, 06:24:13 PM by Bill-Bo
andy,

Something is wrong. I get zeroes for all results.

Hey, I went back and re-ran it. I did decimal 45 to base 2 and got 000001; 60 to base 35, got 00 (should be 1P); back to 45 to base 2 and this time got 00000. If I re-run, it does the same thing. This is weird.

Bill

Andy

March 13, 2016, 11:49:17 PM #12 Last Edit: March 14, 2016, 07:15:19 AM by andy1966
Bill,

That's very strange....

I ran 45 (dec) to base 2 and got 101101
and  60 (dec) to base 35 and got 1P,
I went back and then did 45 (dec) to base 2 and got 101101 again.

I even downloaded the zip file to a new folder, copied the lib file over to my IWBDev /libs folder, compiled the new downloaded ToBase.iwb and it again worked correctly.

The results are correct on my machine.

I wonder why It's not correct on your machine?  ???

Andy.


Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

Andy

Bill,

I found one string which I had not declared ("groan"), and I have amended that in this new zip file.

Could you please download it and have another go.

But that said, the origional program did still work for me with no problems.

Thanks,
Andy.

Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

Bill-Bo

March 14, 2016, 06:17:28 AM #14 Last Edit: March 14, 2016, 09:11:47 AM by Bill-Bo
andy

I do not understand. It does the same thing. I'm going to try something. Get back with you later.

I'm back. I tried the old inc and lib. No good, would not compile. Reloaded the lib and still would not compile. I reloaded the file LarryMc did for me above. It compile and ran just fine, again.

I have attached the files IWBasic created yesterday when it compiled your update. Also, a screenshot of dec 45 to base 2 being 00001. The Output Window.txt is the error info when I tried to recompile.

Bill

Egil

March 14, 2016, 10:56:28 AM #15 Last Edit: March 14, 2016, 10:59:03 AM by Egil
Bill,

First I got the same error message as you, when trying to compile Andy's latest code.
Then I noticed that Andy also included an updated version of the file "StringMap.lib".
When this lib-file was copied into IWB's lib-folder (replacing an older version), the code compiled ok, and produced the same results as in Andy's examples when the exe was run.

Support Amateur Radio  -  Have a ham  for dinner!

Bill-Bo

Egil,

Okay, I rebuilt with the new lib. I ran it and it still has the same problem. Just like my attached screenshot. When I close the program and restart, I get all zeroes for everything.

Bill

Andy

Egil,

Thanks for checking me (great to hear from you again by the way!).

Bill,

Attached are screenshots from my results - they are correct - I still cannot work out why It's wrong for you.

Andy.
:)
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

Bill-Bo

andy,

I cannot get it to operate properly. I made sure that only the files in your BaseV2.zip were in my IWBDev3 directory. I do not understand what's going on. Why everybody else can compile and run it okay; just me that's having the problems. But the thing is I can load to program above that LarryMc modified for and build and run just fine.

Bill

Andy

March 15, 2016, 01:29:28 AM #19 Last Edit: March 15, 2016, 01:33:34 AM by Andy
Bill,

Reading back Egil had the same problem but realised that he was using an old verison of the .lib file.

So I've amended the library file so it can display what version I have set it to (which is currently version 7)

Bill, can we go back to basics please?

1. Please delete StringMap.lib from your C:\Program Files\IWBDev3\Libs folder.
2. Please delete StringMap.inc from your C:\Program Files\IWBDev3\include folder (if it exists there).
3. Create a new folder on your C: drive.
4. Download the attached file to the new folder.
5. Unzip it.
6. Copy the StringMap.lib file to C:\Program Files\IWBDev3\Libs folder.
7. Copy the StringMap.inc file to C:\Program Files\IWBDev3\include folder.
8. Go back to the new folder you created.
9. Compile and run ToBase.iwb

You should see it says - StringMap Library version 7 at the top of the console.

I have followed the above step by step instructions and the program works correctly.

Please let me know how you get on as I would like to understand why you have had problems.

Thanks,
Andy.



 
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

Bill-Bo

andy,

I had already did what you suggested yesterday before my previous post. Here is what I did this morning. You will find this interesting, plus, confusing.

First off, the laptop I currently use is a Windows 8.1 64-bit. I put the zip on one of my memory sticks and put it on one of my other laptops; Windows 7 64-bit. On it I have IWBasic 2.5. I did the program on it and it ran just fine. I copied it to the stick and ran the tobase.exe. It did not work correctly. I restarted my computer and it did the same. So I figure it must be something to do with Windows 8.1.

Bill

Andy

March 15, 2016, 06:37:35 AM #21 Last Edit: March 15, 2016, 06:46:20 AM by Andy
Bill,

That's very interesting, thanks for trying out what you did, I know it must have taken some time!!!

I'm on Win 7 32 bit here, it would be interesting to see if whatever it is has been fixed in Windows 10.

Could someone with windows 10 please try it out for me please and let me know.

Also, I wonder why it doesn't work on Win 8.1  ???

I've put a lot of work into my StringMap library, (for my own enjoyment) and for now, I know most users probably don't need it, but I know at some point it will save someone a lot of work - It's becoming almost a language "inside" another one - IWB.

I would edge towards it being a fault with 8.1 rather than IWB - maybe that's why they want everyone to jump to Win 10 now???

Curious!

Thanks,
Andy.
:)
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

Egil

Quote from: Bill-Bo on March 15, 2016, 06:30:50 AM
So I figure it must be something to do with Windows 8.1.

When I bought the 64 bits tabletop I'm using right now, it came with Windows 8.1. None of the code I compiled with IWB, EB or MiniBASIC would ever work. Most of them did compile though, but the EXE-files did not work as expected.
I was told my Micro$oft in Oslo to turn off the UAC. But the only difference was that I then got warnings when trying to run my own programs. And then they "froze".
So I purchased and installed Windows 7 Pro instead, and all my trouble was gone.

Last summer I backed up the Win7 setup, and installed Windows 10 instead. But the result was that the problems I had with Windows 8.1 returned. And in addition none of my multimedia editing software (which I use a lot) would ever work.

So finally I had to roll back the new install, and use Windows 7 instead. Therefore, if they do not fix Windows 10 in such a way  that my homemade programs are accepted by the system, something that I am convinced will NEVER happen, the setup I use right now will be my last Windows system ever.

Now I am exploring how to use Linux instead, and when I find the right language/compiler package, I'll change whithout hesitation because I do not need Micro$soft any more. Or maybe I'll do as my oldest grandson. He has a lot of fun, using a 30 dollars RaspberryPI hooked to a cheap "smart-TV". Doing pretty much the same with that setup as I do with mine 2000 dollars setup.

If anyone contact me by PM, I'll answer and tell them where to put both Micro$oft and their Windows systems.... >:(
Support Amateur Radio  -  Have a ham  for dinner!

billhsln

Egil, just curious, what anti-virus program do you use?  I am running windows 10 Pro and have no problems with any of the programs I have written.  I use Eset Nod 32 for my Anti-Virus program and have not had that many problems.  Most of the time when I get the big error, it is due to memory problems (like doing a read into a string variable when I should be using ISTRING defined much larger than 255).  I have also run into it when there was a bug in the program.

Bill
When all else fails, get a bigger hammer.

Andy

Bill,

Any chance you could download the last zip file and try it for me (BaseVer.zip) ?

It would really help me to understand what works on what and what doesn't.

P.S. I have had problems with Avast A/V myself lately.

Thanks,
Andy.
:)
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.