VBAndCs / sVB-Small-Visual-Basic

Small Visual Basic (sVB) is an educational programming language, created by Eng. Mohammad Hamdy as an evolved version of Microsoft Small Basic (SB). It is meant to be easier and more powerful at the same time, to introduce programming basics to kids and beginners of any age, provided that they can use the English keyboard on the Windows OS.
Other
233 stars 16 forks source link

operator "mod" and "%" evaluates to the same result #35

Closed dynamicboy closed 5 months ago

dynamicboy commented 12 months ago

Verson: Small Visual Basic v2.8.7 Details: the operator "mod" is supposed to get the quotient the operator "%" is supposed to get the remainder

But I get the same result with hat "mod" and "%" .

Small Visual Basic Evaluator.Expression = "x % 10" TW.WriteLine(Evaluator.Evaluate(87)) ' output is 7

Evaluator.Expression = "x mod 10" TW.WriteLine(Evaluator.Evaluate(87)) ' output is 7

CodeScreenShot.png

ResultScreenShot.png

VBAndCs commented 12 months ago

the operator "mod" is supposed to get the quotient

No. The Mod operator in VB, VB.NET, SB, and sVB returns the remainder. The % does the same in non BASIC languages like C++, C#, Python… etc, so I decided to add it as an alternative option in the math evaluator (but not in sVB code).

The \ operator (not /) is the integral operator in VB and VB.NET, but it is not supported in SB nor sVB. Instead you should use the Math methods Like Math.Floor(), Math.Ceiling() or math.Round() to get the integer part. I realized now there is no Math.Truncate() method, but you can get its result by calling Math.Floor(Math.Abs(x)). In the Evaluator you can just write:

Floor(Abs(x/3)) which will give you the result of x\3.

I will add the missing Truncate method, and I may consider adding the missing integral operator to sVB too.

Thanks.


From: dynamicboy @.> Sent: Wednesday, November 29, 2023 9:34 PM To: VBAndCs/sVB-Small-Visual-Basic @.> Cc: Subscribed @.***> Subject: [VBAndCs/sVB-Small-Visual-Basic] operator "mod" and "%" evaluates to the same result (Issue #35)

Verson: Small Visual Basic v2.8.7 Details: the operator "mod" is supposed to get the quotient the operator "%" is supposed to get the remainder

But I get the same result with hat "mod" and "%" .

Small Visual Basic Evaluator.Expression = "x % 10" TW.WriteLine(Evaluator.Evaluate(87)) ' output is 7

Evaluator.Expression = "x mod 10" TW.WriteLine(Evaluator.Evaluate(87)) ' output is 7

CodeScreenShot.pnghttps://postimg.cc/FdxkrDGQ

ResultScreenShot.pnghttps://postimg.cc/PC66xQ3Y

— Reply to this email directly, view it on GitHubhttps://github.com/VBAndCs/sVB-Small-Visual-Basic/issues/35, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALQ5MVTU4O5IHJI7YTLFDA3YG6S6TAVCNFSM6AAAAABAAEY7A2VHI2DSMVQWIX3LMV43ASLTON2WKOZSGAYTONBVGQZTMMA. You are receiving this because you are subscribed to this thread.Message ID: @.***>

VBAndCs commented 12 months ago

Correction:

Floor(abs()) will change thee sign of the negative number. Seem that no alternative for Truncate.


From: dynamicboy @.> Sent: Wednesday, November 29, 2023 9:34 PM To: VBAndCs/sVB-Small-Visual-Basic @.> Cc: Subscribed @.***> Subject: [VBAndCs/sVB-Small-Visual-Basic] operator "mod" and "%" evaluates to the same result (Issue #35)

Verson: Small Visual Basic v2.8.7 Details: the operator "mod" is supposed to get the quotient the operator "%" is supposed to get the remainder

But I get the same result with hat "mod" and "%" .

Small Visual Basic Evaluator.Expression = "x % 10" TW.WriteLine(Evaluator.Evaluate(87)) ' output is 7

Evaluator.Expression = "x mod 10" TW.WriteLine(Evaluator.Evaluate(87)) ' output is 7

CodeScreenShot.pnghttps://postimg.cc/FdxkrDGQ

ResultScreenShot.pnghttps://postimg.cc/PC66xQ3Y

— Reply to this email directly, view it on GitHubhttps://github.com/VBAndCs/sVB-Small-Visual-Basic/issues/35, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALQ5MVTU4O5IHJI7YTLFDA3YG6S6TAVCNFSM6AAAAABAAEY7A2VHI2DSMVQWIX3LMV43ASLTON2WKOZSGAYTONBVGQZTMMA. You are receiving this because you are subscribed to this thread.Message ID: @.***>

dynamicboy commented 12 months ago

@VBAndCs Thank you. I used "Math.Floor( dividend / 10)" to get the integral part as an alternative. I just came across to this by trying to convert an integer to a string with a given width (12 -> 0012),as the "Text" library doesn't have such functions. I wrote one as below:


Function IntToStr(dbl_num, dbl_width)
   resultStr = ""

   digits = {}

   dividend = dbl_num
   quotient = 1
   remainder = 0

   While quotient <> 0
      quotient = Math.Floor(dividend / 10)
      remainder = Math.Remainder(dividend, 10)

      digits = digits.AddItem(remainder)

      dividend = quotient
   Wend

   fillSpaces = dbl_width - digits.Count

   If fillSpaces > 0 Then
      For i = 1 To fillSpaces
         resultStr = resultStr.Append("0")
      Next

   EndIf

   For i = digits.Count To 1 Step -1
      resultStr = resultStr.Append(digits.GetItemAt(i))
   Next

   Return resultStr
EndFunction
VBAndCs commented 12 months ago

You don't need division and remainder for padding. Look at this code. You may do changes if you don't want to count the dot as a char, or create two different functions: LeftPad and RighPad if you want to add zeros at only one direction, and in the Right pad you should add the dot if it doesn't exist.

TW.WriteLines({
      IntToStr(15, 5),
      IntToStr(1532, 5),
      IntToStr(15329, 5),
      IntToStr(15.5, 5),
      IntToStr(15.32, 5),
      IntToStr(0.32, 5),
      IntToStr(0.329, 5),
      IntToStr(15.329, 5),
      IntToStr(0.32901, 5)
   })

TW.WriteLine("---------------")
TW.WriteLines({
      IntToStr(15, 7),
      IntToStr(1532, 7),
      IntToStr(15329, 7),
      IntToStr(15.5, 7),
      IntToStr(15.32, 7),
      IntToStr(0.32, 7),
      IntToStr(0.329, 7),
      IntToStr(15.329, 7),
      IntToStr(0.32901, 7)
   })

'------------------------------------------------
Function IntToStr(dbl_num, dbl_width)
   len = Text.GetLength(dbl_num)
   padlen = dbl_width - len

   If padlen = 0 Then
      Return dbl_num
   EndIf

   pos = Text.GetIndexOf(dbl_num, ".", 1, False)
   If padlen < 0 Then
      Return dbl_num.Round(len - pos + padlen)
   EndIf

   pad = {}
   For i = 1 To padlen
      pad.Append("0")
   Next

   zeros = pad.Join("")
   If pos = 0 Then
      Return Text.Append(zeros, dbl_num)
   EndIf

   Return Text.Append(dbl_num, zeros)
EndFunction

From: dynamicboy @.> Sent: Thursday, November 30, 2023 1:58 PM To: VBAndCs/sVB-Small-Visual-Basic @.> Cc: Mohammad Hamdy Ghanem @.>; Mention @.> Subject: Re: [VBAndCs/sVB-Small-Visual-Basic] operator "mod" and "%" evaluates to the same result (Issue #35)

@VBAndCshttps://github.com/VBAndCs Thanks you. I used "Math.Floor( dividend / 10)" to get the integral part as an alternative. I just came across to this by trying to convert an integer to a string with a given width (12 -> 0012),as the "Text" library doesn't have such functions. I wrote one as below:

Function IntToStr(dbl_num, dbl_width) resultStr = ""

digits = {}

dividend = dbl_num quotient = 1 remainder = 0

While quotient <> 0 quotient = Math.Floor(dividend / 10) remainder = Math.Remainder(dividend, 10)

  digits = digits.AddItem(remainder)

  dividend = quotient

Wend

fillSpaces = dbl_width - digits.Count

If fillSpaces > 0 Then For i = 1 To fillSpaces resultStr = resultStr.Append("0") Next

EndIf

For i = digits.Count To 1 Step -1 resultStr = resultStr.Append(digits.GetItemAt(i)) Next

Return resultStr EndFunction

— Reply to this email directly, view it on GitHubhttps://github.com/VBAndCs/sVB-Small-Visual-Basic/issues/35#issuecomment-1833834479, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALQ5MVV3BVGVNGGUTK4QZILYHCGJLAVCNFSM6AAAAABAAEY7A2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMZTHAZTINBXHE. You are receiving this because you were mentioned.Message ID: @.***>

VBAndCs commented 12 months ago

And this is how you can get the integral and decimal parts of a number:

X = 10.102
Parts = Text.Split(X, ".", True, False)
TextWindow.WriteLines({
      "integer: " + Parts[1],
      "decimal: " + Parts[2]
})

From: dynamicboy @.> Sent: Thursday, November 30, 2023 1:58 PM To: VBAndCs/sVB-Small-Visual-Basic @.> Cc: Mohammad Hamdy Ghanem @.>; Mention @.> Subject: Re: [VBAndCs/sVB-Small-Visual-Basic] operator "mod" and "%" evaluates to the same result (Issue #35)

@VBAndCshttps://github.com/VBAndCs Thanks you. I used "Math.Floor( dividend / 10)" to get the integral part as an alternative. I just came across to this by trying to convert an integer to a string with a given width (12 -> 0012),as the "Text" library doesn't have such functions. I wrote one as below:

Function IntToStr(dbl_num, dbl_width) resultStr = ""

digits = {}

dividend = dbl_num quotient = 1 remainder = 0

While quotient <> 0 quotient = Math.Floor(dividend / 10) remainder = Math.Remainder(dividend, 10)

  digits = digits.AddItem(remainder)

  dividend = quotient

Wend

fillSpaces = dbl_width - digits.Count

If fillSpaces > 0 Then For i = 1 To fillSpaces resultStr = resultStr.Append("0") Next

EndIf

For i = digits.Count To 1 Step -1 resultStr = resultStr.Append(digits.GetItemAt(i)) Next

Return resultStr EndFunction

— Reply to this email directly, view it on GitHubhttps://github.com/VBAndCs/sVB-Small-Visual-Basic/issues/35#issuecomment-1833834479, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALQ5MVV3BVGVNGGUTK4QZILYHCGJLAVCNFSM6AAAAABAAEY7A2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMZTHAZTINBXHE. You are receiving this because you were mentioned.Message ID: @.***>

dynamicboy commented 12 months ago

I didn't realize that numbers could be used as a string. It will be nice to add a ToString("formatString") method to numbers.

I haven't learn all content of sVB reference book. I just look it up when I'm about to use particular feature.

My Path along with Basic language is: QBasic, QB64, Basic256, Microsoft Small Basic, Small Basic Prime, finally the Small Visual Basic。

I tried to make a few programs with each of them and found out It's hard and time-consuming to present a pretty UI, and difficult to make really useful programs without extensions.

Small Visual Basic is the most convinient one to make pretty UIs. The auto-completion makes coding fast and enjoyable.

It's worth promoting more.

BTW, I found that Sound.Play() is probably playing in background thread, it can play several songs at the same time without making the program waiting. Is there any way to dispatch an operation, such as File.Copy, to a background thread to keep UI active? And with Sound, how to get the length of mp3 file in order to know when it stopped.

Thanks for your contribution.

VBAndCs commented 12 months ago

Thanks for your kind words.

SB and so sVB are dynamically typed. It only contains one type which is the "Primitive" which can hold a string, a number, or an array. It can do all necessary conversions between these primitive types, so, you can say all types in sVB including arrays, forms and controls are actually represented by strings, because there is no objects in SB nor sVB, but sVB makes you feel that there are objects and do all the underling work to convert the object syntax to a call to a static method with the object name as the string key sent to the first parameter. So, don't be afraid to try dealing with any thing in sVB as a string if necessary.

I understand your need to more functionality in sVB library, but note that sVB is an educational tool not a commercial product, and it doesn't intend to reinvent the wheel and replicate VB .NET and the .NET Class library because they are already available and because this will defy the sVB purpose as and easy step up form SB to VB.NET and the dotnet platform.

So, I tried to keep it small as I could, but it ended up with a 750 pages reference book! This is too much for Kids and beginners, and I can't make it harder.

For example, none asked me yet why I prevented the user from resizing the form at runtime. This is because if I did so, programmers will ask me to add a punch of properties, methods, and events to deal with the resizing process, the form control boxes and border styles.. etc. I see no need for all this complication in an educational language, and when a student wants to be a professional programmer and create commercial apps, sVB then accomplished its mission and its time to move on to VB .NET.

Also, the SB a compiler is not efficient and so the sVB compiler that is built on top of it, because the primitive type makes it slow and memory consuming.

So, try enjoy sVB as it, and use it to learn as much as it can give, before taking the next step to VB.NET, and believe me, you will find it so familiar and you will easily go through it.

Thanks.


From: dynamicboy @.> Sent: Friday, December 1, 2023 3:20 PM To: VBAndCs/sVB-Small-Visual-Basic @.> Cc: Mohammad Hamdy Ghanem @.>; Mention @.> Subject: Re: [VBAndCs/sVB-Small-Visual-Basic] operator "mod" and "%" evaluates to the same result (Issue #35)

I didn't realize that numbers could be used as a string. It will be nice to add a ToString("formatString") method to numbers.

I haven't learn all content of sVB reference book. I just look it up when I'm about to use particular feature.

My Path along with Basic language is: QBasic, QB64, Basic256, Microsoft Small Basic, Small Basic Prime, finally the Small Visual Basic。

I tried to make a few programs with each of them and found out It's hard and time-consuming to present a pretty UI, and difficult to make really useful programs without extensions.

Small Visual Basic is the most convinient one to make pretty UIs. The auto-completion makes coding fast and enjoyable.

It's worth promoting more.

BTW, I found that Sound.Play() is probably playing in background thread, it can play several songs at the same time without making the program waiting. Is there any way to dispatch an operation, such as File.Copy, to a background thread to keep UI active? And with Sound, how to get the length of mp3 file in order to know when it stopped.

Thanks for your contribution.

— Reply to this email directly, view it on GitHubhttps://github.com/VBAndCs/sVB-Small-Visual-Basic/issues/35#issuecomment-1836295732, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALQ5MVWOCUORM75E3SR335LYHHYUPAVCNFSM6AAAAABAAEY7A2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMZWGI4TKNZTGI. You are receiving this because you were mentioned.Message ID: @.***>