vectorgraphics / asymptote

2D & 3D TeX-Aware Vector Graphics Language
https://asymptote.sourceforge.io/
GNU General Public License v3.0
533 stars 89 forks source link

remainder function in the math module #463

Closed thejohnrainwater closed 2 weeks ago

thejohnrainwater commented 2 weeks ago

There seems to be a bug in the remainder function of the math module. When given a divisor parameter of 2 for dividends of all integers, the function returns a value of -1 for some natural integers; of course, the only returned values for dividing any integer by 2 should be either 0 or 1. This is for positive integers; that's all I've found. thanks!

johncbowman commented 2 weeks ago

Please post an example that demonstrates such an unsual behaviour, along with the platform and OS you are running under.

thejohnrainwater commented 2 weeks ago

Hi Dr. Bowman -

When I reported a potential bug, I was using the remainder function wrapped in another function which was rounding off my numbers, so first i wanted to test it out without all the noise; however, it's still reproducing the same results - for some integers (a lot of them) - output is -1 for the divisor of 2 on some positive integers.

Here is the Asymptote code I just tested it again with, along with a screenshot of the first 3-4 outputs of -1 (both attached).

Do you want me to also post this to my bug claim on GitHub?

I have a gut feeling this could be related to the remainder function giving real valued output, but my integers are declared as integers. I tried checking with changing my loop indices to real values (0.0 and 1.0 rather than 0,1), but it doesn't change anything.

Tthanks,

john

On Thu, Jun 20, 2024 at 4:01 PM John Bowman @.***> wrote:

Please post an example that demonstrates such an unsual behaviour, along with the platform and OS you are running under.

— Reply to this email directly, view it on GitHub https://github.com/vectorgraphics/asymptote/issues/463#issuecomment-2181682996, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGAJTQO4C55KW7FSQYWIV73ZINNN5AVCNFSM6AAAAABJUYLLGCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBRGY4DEOJZGY . You are receiving this because you authored the thread.Message ID: @.***>

johncbowman commented 2 weeks ago

Could you please attach the Asymptote code and its output.

If by chance you are referring to the POSIX double remainder(double x, double y); function that Asymptote gives you access to, you are using the wrong function. You want to use the integer modulo operator % just like in C or C++. If you need integer division, use the operator #.

johncbowman commented 2 weeks ago

P.S. Unlike in C and C++, Asymptote uses a portable implementation definition of % so that the result has the same sign as the divisor.

thejohnrainwater commented 2 weeks ago

here it is again, not sure why it didn't come through before,,,sorry!

john

On Thu, Jun 20, 2024 at 5:58 PM John Bowman @.***> wrote:

Could you please attach the Asymptote code and its output.

If by chance you are referring to the POSIX double remainder(double x, double y); function that Asymptote, you are using the wrong function. You want to use the integer modulo function % just like in C or C++. If you need integer division, use the operator #.

— Reply to this email directly, view it on GitHub https://github.com/vectorgraphics/asymptote/issues/463#issuecomment-2181796416, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGAJTQJ4K2W23JJCU3PUEJLZIN3CLAVCNFSM6AAAAABJUYLLGCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBRG44TMNBRGY . You are receiving this because you authored the thread.Message ID: @.***>

johncbowman commented 2 weeks ago

Nothing got attached. Since your attachments are being rejected, maybe try inserting the code directly in the text box in code mode (apply <> to the region).

thejohnrainwater commented 2 weeks ago

That is so odd because it shows in the emails I sent in my sent folder that those attachments were sent. Maybe your email server is rejecting them for some reason...Here it is in line:

CODE

import math;

for(int i=1; i<10000; ++i) { real r = remainder(i,2); if (r != 0 && r != 1) { write(i,r); breakpoint(); } }

OUTPUT FOR FIRST FEW -1 INSTANCES

[image: image.png]

You're right (of course) about the fact that I was using the wrong function; I should have been using % or maybe fmod, but I just expected the remainder function to output the same as those functions. After all, i mod 2 = [0, 1] all the time for integers, so I thought remainder (i, 2) would too. They are booth remainders.

Thanks,

john

On Thu, Jun 20, 2024 at 7:11 PM John Bowman @.***> wrote:

Nothing got attached. Since your attachments are being rejected, maybe try inserting the code directly in the text box in code mode (apply <> to the region).

— Reply to this email directly, view it on GitHub https://github.com/vectorgraphics/asymptote/issues/463#issuecomment-2181851581, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGAJTQIHB3KCHVT37BMZ3L3ZIODWPAVCNFSM6AAAAABJUYLLGCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBRHA2TCNJYGE . You are receiving this because you authored the thread.Message ID: @.***>

thejohnrainwater commented 2 weeks ago

While i'm at interrogating your expertise, may i ask why the 'sys' module cannot be imported? Some of its functions work without import (sleep) but some don't (usleep). I was looking for a higher resolution timer than seconds() from Epoch...

thanks,

john

On Thu, Jun 20, 2024 at 7:11 PM John Bowman @.***> wrote:

Nothing got attached. Since your attachments are being rejected, maybe try inserting the code directly in the text box in code mode (apply <> to the region).

— Reply to this email directly, view it on GitHub https://github.com/vectorgraphics/asymptote/issues/463#issuecomment-2181851581, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGAJTQIHB3KCHVT37BMZ3L3ZIODWPAVCNFSM6AAAAABJUYLLGCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBRHA2TCNJYGE . You are receiving this because you authored the thread.Message ID: @.***>

johncbowman commented 2 weeks ago

As I suspected, you are using real remainder(real, real) for integer arithmetic! That is a big no no in scientific computing, due to finite floating point precision. Use % instead.

Regarding "sys" there is no such Asymptote module; perhaps you are confusing Asymptote with Python.

Anyway, both sleep and usleep are available:

 asy
Welcome to Asymptote version 2.90-69 (to view the manual, type help)
> sleep
<void sleep(int seconds)>
> usleep
<void usleep(int microseconds)>
> 
thejohnrainwater commented 2 weeks ago

Thanks. i apolgize, i knew usleep is available, what i meant is systime function, which is said online somewhere to report milliseconds since Epoich...is that really a function in Asymptote? I'm just looking more a timer with more resolution than seconds...

thanks,

john

On Thu, Jun 20, 2024 at 8:29 PM John Bowman @.***> wrote:

As I suspected, you are using real remainder(real, real) for integer arithmetic! That is a big no no in numerical analysis, due to finite floating point precision. Use % instead.

Regarding "sys" there is no such Asymptote module; perhaps you are confusing Asymptote with Python.

Anyway, both sleep and usleep are available:

asy Welcome to Asymptote version 2.90-69 (to view the manual, type help)

sleep <void sleep(int seconds)> usleep <void usleep(int microseconds)>

— Reply to this email directly, view it on GitHub https://github.com/vectorgraphics/asymptote/issues/463#issuecomment-2181934457, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGAJTQIUFSY34WLYOHAVKOLZIOM2RAVCNFSM6AAAAABJUYLLGCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBRHEZTINBVG4 . You are receiving this because you authored the thread.Message ID: @.***>

johncbowman commented 2 weeks ago

Do you really need to know the absolute number of milliseconds after the epoch? Isn't the nanosecond clock in cputime().parent.user or cputime().parent.clock sufficient?

thejohnrainwater commented 2 weeks ago

well that would be better actually higher resolution but i have no idea what cputime().parent.user or cputime().parent.clock are? Are those Asymptote functions?

thanks,

john

On Thu, Jun 20, 2024 at 8:50 PM John Bowman @.***> wrote:

Do you really need to know the absolute number of milliseconds after the epoch? Isn't the nanosecond clock in cputime().parent.user or cputime().parent.clock sufficient?

— Reply to this email directly, view it on GitHub https://github.com/vectorgraphics/asymptote/issues/463#issuecomment-2181950648, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGAJTQIZCI46HHEKZLW6OILZIOPIZAVCNFSM6AAAAABJUYLLGCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBRHE2TANRUHA . You are receiving this because you authored the thread.Message ID: @.***>

thejohnrainwater commented 2 weeks ago

Wow! So they are...that works, yes thanks!

john

On Thu, Jun 20, 2024 at 8:50 PM John Bowman @.***> wrote:

Do you really need to know the absolute number of milliseconds after the epoch? Isn't the nanosecond clock in cputime().parent.user or cputime().parent.clock sufficient?

— Reply to this email directly, view it on GitHub https://github.com/vectorgraphics/asymptote/issues/463#issuecomment-2181950648, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGAJTQIZCI46HHEKZLW6OILZIOPIZAVCNFSM6AAAAABJUYLLGCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBRHE2TANRUHA . You are receiving this because you authored the thread.Message ID: @.***>

thejohnrainwater commented 2 weeks ago

Sorry, how do I access those functions from code blocks, rather than just command-line Asymptote? Are they part of a module I have to import? Because I'm getting errors when I just tro to use those as follows:

for(int i=0; i<10000; ++i) { real a = cputime().parent.user; real b = cputime().parent.clock; write(a); write(b); }

thanks Dr. Bowman!

john

On Thu, Jun 20, 2024 at 8:50 PM John Bowman @.***> wrote:

Do you really need to know the absolute number of milliseconds after the epoch? Isn't the nanosecond clock in cputime().parent.user or cputime().parent.clock sufficient?

— Reply to this email directly, view it on GitHub https://github.com/vectorgraphics/asymptote/issues/463#issuecomment-2181950648, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGAJTQIZCI46HHEKZLW6OILZIOPIZAVCNFSM6AAAAABJUYLLGCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBRHE2TANRUHA . You are receiving this because you authored the thread.Message ID: @.***>

thejohnrainwater commented 2 weeks ago

i still haven't figured out what those 2 functions really output, but i did find my error in the last code block i sent you. Please disregard that email/question, sorry about that...

john

On Thu, Jun 20, 2024 at 8:50 PM John Bowman @.***> wrote:

Do you really need to know the absolute number of milliseconds after the epoch? Isn't the nanosecond clock in cputime().parent.user or cputime().parent.clock sufficient?

— Reply to this email directly, view it on GitHub https://github.com/vectorgraphics/asymptote/issues/463#issuecomment-2181950648, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGAJTQIZCI46HHEKZLW6OILZIOPIZAVCNFSM6AAAAABJUYLLGCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBRHE2TANRUHA . You are receiving this because you authored the thread.Message ID: @.***>

johncbowman commented 2 weeks ago

https://asymptote.sourceforge.io/doc/Structures.html#index-cputime

thejohnrainwater commented 2 weeks ago

Hi Dtr. Bowman -

Thanks for the reference, I missed that when I was looking into the documentation.

I just want to make one comment about Asymptote: I am finding it to be absolutely incredible and more useful than most any other mathematical tool I've found so far. Its functionality is unparalleled, it's use is largely intuitive, scripting and programming features are epic, and the learning curve is minimal. It's very powerful. I was only recently introduced to Asymptote as I started consulting for a company who hired me to help train large language AI models in mathematical reasoning. They are using Asymptote as a key core part of training their models, so over the past several months I have used Asymptote extensively for both generating LLM prompts and solving those prompts using Asymptote as well (and a bit of Matlab now and then). I simply love it, it's power and feature rich protocol are both integral parts in helping to shape our very future as a society, as we are at the genesis of the largest paradigm shift perhaps in human history with AI and machine learning developments finally becoming commercially available.

Asymptote is simply a visionary masterpiece, you, Sir, are a clear genius, and I applaud your genius for developing this language. Thank you!

Very Respectfully,

john

On Thu, Jun 20, 2024 at 11:13 PM John Bowman @.***> wrote:

https://asymptote.sourceforge.io/doc/Structures.html#index-cputime

— Reply to this email directly, view it on GitHub https://github.com/vectorgraphics/asymptote/issues/463#issuecomment-2182073710, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGAJTQPK5ODATNO5EFU2Q73ZIPAALAVCNFSM6AAAAABJUYLLGCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBSGA3TGNZRGA . You are receiving this because you authored the thread.Message ID: @.***>

thejohnrainwater commented 2 weeks ago

Dr. Bowman -

I'm sorry to bug you again, but I keep getting this error whenever I try to compile some of the code in the examples folder, for example, axis3.asy.

C:\Program Files\gs\gs10.03.1\bin\gswin64.exe -q -dBATCH -P -dSAFER -sDEVICE=ps2write -sOutputFile=NUL 999_.ps pathcache.insert(j,g[i]); ^plain_Label.asy: 679.23: reading array of length 0 with out-of-bounds index 0

Can you tell me why I keep seeing this? Is it a Ghostscript problem (I tried uninstalling and reinstalling that, but there was no change), or is it an issue with a versioning mismatch between my core Asymptote or plain_Label.asy file, or something entirely different? I mean, I'm just copying the code from the examples and trying to execute it verbatim as written.

Thank you in advance for your help. Don't worry, I'm not going to bug you about every little problem I encounter; it's just that I've seen this error since day 1, and it's preventing me from using a lot of these functions because the examples won't compile, so it has been difficult to figure out otherwise, and I've saturated the chat board online to no avail on this issue.

Cheers!

john

On Thu, Jun 20, 2024 at 11:13 PM John Bowman @.***> wrote:

https://asymptote.sourceforge.io/doc/Structures.html#index-cputime

— Reply to this email directly, view it on GitHub https://github.com/vectorgraphics/asymptote/issues/463#issuecomment-2182073710, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGAJTQPK5ODATNO5EFU2Q73ZIPAALAVCNFSM6AAAAABJUYLLGCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBSGA3TGNZRGA . You are receiving this because you authored the thread.Message ID: @.***>

johncbowman commented 2 weeks ago

What version of Asymptote are you using?

thejohnrainwater commented 2 weeks ago

2.89

On Fri, Jun 21, 2024 at 11:52 John Bowman @.***> wrote:

What version of Asymptote are you using?

— Reply to this email directly, view it on GitHub https://github.com/vectorgraphics/asymptote/issues/463#issuecomment-2183288158, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGAJTQLZQQVPUHZMQATGLXLZIRZAPAVCNFSM6AAAAABJUYLLGCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBTGI4DQMJVHA . You are receiving this because you authored the thread.Message ID: @.***>

johncbowman commented 2 weeks ago

gswin64.exe is wrong; it should read gswin64c.exe Did you override the default setting in your config.asy file, perhaps?

thejohnrainwater commented 2 weeks ago

My God, you're amazing!! Indeed, I don't recall why now, but I had added a line to my config.asy file that pointed to the wrong GhostScript executable, just exactly as you deduced. So I'm showing you an image of a matrix generated by Asymptote, which represents an arbitrary 3D adjacency matrix whose values at each coordinate are 0 or 1—red or blue. Our model will use matrices similar to this very small one to determine relationships between traits and features of systems like the human body and physiology, cellular components inside cells, DNA sequences, and much more. Some of our matrixes will have many, many more dimensions, but the idea is the same. This is how your language is being used by us for AI, and as I said before, it is invaluable, so thanks again, sir!

take care!

john

[image: image.png]

On Fri, Jun 21, 2024 at 12:11 PM John Bowman @.***> wrote:

gswin64.exe is wrong; it should read gswin64c.exe Did you override the default setting in your config.asy file, perhaps?

— Reply to this email directly, view it on GitHub https://github.com/vectorgraphics/asymptote/issues/463#issuecomment-2183312098, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGAJTQKQFK72BDR6HSNYEG3ZIR3HHAVCNFSM6AAAAABJUYLLGCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBTGMYTEMBZHA . You are receiving this because you authored the thread.Message ID: @.***>

thejohnrainwater commented 1 week ago

Hi Dr. Bowman,

I hope you had a great weekend! I hate to bother you again with another question, and I did promise not to bug you about every little problem I might encounter, but I've read the documentation on Arrays and Structs 100 times now and tried everything I could think of trying (maybe there's more) on this with no success yet. If you haven't already figured it out, and I know you didn't ask, but I am not a professional programmer, not by any stretch of the imagination, no. I'll be a first-year graduate student this Fall, my first semester in pursuit of my doctorate in Applied Mathematics at Stanford. I'm also 54 years old and have worked for the past 28 years as a Laser Development Engineer, in which I did learn some Python and C coding but mostly for very simple projects in laser manufacturing—something real programmers would call impressive. Last year, when the company I was working for was acquired, the new leadership team decided to move my job to Europe. Since I had always wanted to finish graduate school before my journey ends, I took the layoff as my entry point into doing so, so here I am—the oldest graduate student and card-carrying AARP member in the student body. I mentioned to you I was hired by an AI company to train some large language models in math reasoning, so here I am. I'm fairly comfortable with my math background, but I knew I would be on a learning curve in terms of programming background. So I'm learning now, or trying to. You might have deduced from my previous questions that I was an amateur programmer, and if so, you would have been correct. But I'll get there, hopefully before I'm ready to retire, which isn't really too far off.

What I'm trying to do may or may not be possible in the asymptote; I'm uncertain, so all I want to ask you is, which is it—yes, it's possible, or no, it isn't possible, so don't waste any more time trying because it can't be done? I want to declare multidimensional structs that have mixed argument types. Ideally, there would be no upper bound on dimensions (n), but if only 3-dimensions can be accessed with Asymptote as such—structs with mixed-typed variable parameters—that will be a good start, although eventually I will probably need to find a language to supplement Asymptote and how I'm using it.

So that is my only question: can I setup structs with mixed typed arguments and declare instances of those classes as multidimensional, just as I can arrays as reals or ints or whatever (real[][][] R; int[][][] I; so can I have S s[][][] where S is defined with mixed typed arguments)? If you tell me yes, then great i'll continue trying to figure it out; if not then I'll thank you for steering me away from continuing to bark up the wrong tree. So far, everything I've tried has thrown errors; however, I'm still digesting the last example in the Struct documentation on inheritance, so maybe that has some answers related to what I'm trying to accomplish here?

Well, either way I'll thank you!

Cheers!

john

On Fri, Jun 21, 2024 at 11:52 AM John Bowman @.***> wrote:

What version of Asymptote are you using?

— Reply to this email directly, view it on GitHub https://github.com/vectorgraphics/asymptote/issues/463#issuecomment-2183288158, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGAJTQLZQQVPUHZMQATGLXLZIRZAPAVCNFSM6AAAAABJUYLLGCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBTGI4DQMJVHA . You are receiving this because you authored the thread.Message ID: @.***>

thejohnrainwater commented 1 week ago

Dr. Bowman -

Is it possible in Asymptote to define multidimensional structs as arrays, or to define a struct with mixed-type arguments, then declare instances of that class as 3D, 4D, or n-dim arrays of the struct type? I've read the documentation on structs and arrays many times, but there isn't anything mentioned about defining array structs, per se, so I'm not sure if it's possible. I've already spent a lot of time trying everything I could think of using the documentation references, but nothing so far has worked. I just wonder if this is even doable or not possible with Asymptot. If it isn't, I'd like to stop trying to make it work.

Thank you!

john

On Fri, Jun 21, 2024 at 12:11 PM John Bowman @.***> wrote:

gswin64.exe is wrong; it should read gswin64c.exe Did you override the default setting in your config.asy file, perhaps?

— Reply to this email directly, view it on GitHub https://github.com/vectorgraphics/asymptote/issues/463#issuecomment-2183312098, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGAJTQKQFK72BDR6HSNYEG3ZIR3HHAVCNFSM6AAAAABJUYLLGCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBTGMYTEMBZHA . You are receiving this because you authored the thread.Message ID: @.***>

thejohnrainwater commented 1 week ago

Dr. Bowman -

Disregarded my last email, i'm sorry but as soon as ki sdent thatgqjuestion tol you i figured kjt olut. i apologize.

thank you!

john

On Fri, Jun 21, 2024 at 12:11 PM John Bowman @.***> wrote:

gswin64.exe is wrong; it should read gswin64c.exe Did you override the default setting in your config.asy file, perhaps?

— Reply to this email directly, view it on GitHub https://github.com/vectorgraphics/asymptote/issues/463#issuecomment-2183312098, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGAJTQKQFK72BDR6HSNYEG3ZIR3HHAVCNFSM6AAAAABJUYLLGCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBTGMYTEMBZHA . You are receiving this because you authored the thread.Message ID: @.***>