JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.45k stars 5.46k forks source link

jl_eval_string sin(), cos(), tan() = ErrorException #14666

Closed REMEXChris closed 7 years ago

REMEXChris commented 8 years ago

I have discovered an ErrorException which arises when calling sin() cos() tan() or similar mathematical functions from C via jl_eval_string()

Strangely the degree variants work fine for those instances i.e sind() cosd() tand() however this isn't always the case. atand() for example also results in an error exception. Operators seem to work fine (1*1) for example.

Simple repro case:

jl_eval_string("sin(1.0)"); == ErrorException jl_eval_string("sind(1.0)"); == Works

yuyichao commented 8 years ago

Could you attach the content of the exception?

jl_(ex) or jl_show(jl_stdout_stream(), ex) or jl_static_show(JL_STDOUT, ex)

REMEXChris commented 8 years ago

Hi yuyichao, Thank you for the very quick reply. I am calling from C# by pinvoking a C dll. I will add exception detail now - was wondering how to do this!

I have however, also discovered the problem. The working directory for the C# exe was being set to a folder containing julia scripts not the root julia directory. Changing this has solved it.

Edit - disregard, the error is still there. I will get you the exception details.

REMEXChris commented 8 years ago

Hi yuyichao, Is there an easy way for me to write out the exception details to a const char * so I can pipe it through to the calling C# application? I am unable to reproduce the error directly from my C interface.

yuyichao commented 8 years ago

How did you figured out it's a ErrorException?

REMEXChris commented 8 years ago

const char * GetExceptionString() { return jl_typeof_str(jl_exception_occurred); }

Then on the C# side string ret = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(XXX.GetExceptionString());

This only returns the exceptiontype however, it doesn't give any details.

Thanks, Chris

yuyichao commented 8 years ago

Well, the simplest way is to call jl_(jl_exception_occured()) in C. I have no idea how to do that in c#.

jl_string_data(*(void**)jl_exception_occured()) should get you the char* of the error but I don't know how to do that in c# either....

Another way is to breakpoint jl_throw in the debugger and check where the error is thrown.

yuyichao commented 8 years ago

Also, if you cannot reproduce it in C, it feels like a bug in the c# wrapper.

REMEXChris commented 8 years ago

Ok will keep digging, and let you know once I have full error info.

In the mean time, I have discovered that if I jl_eval_string cd() to change directory immediately after init then I will get errors should I call sin(), cos(), tan() if however I call any of those functions prior to the change directory it works fine, and then continues to work fine once the directory has changed.

Any ideas why this might be?

Thanks for your help so far!

tkelman commented 8 years ago

Probably trying to ccall into openlibm?

tkelman commented 8 years ago

see issue https://github.com/JuliaLang/julia/issues/11419 which this may be a duplicate of

ihnorton commented 7 years ago

Closing for now, please comment and we can re-open if needed. (No follow-up here or on the mailing list, and we've had other people using .NET in the interim)