Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7128679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:09:49+00:00 2026-05-28T11:09:49+00:00

I wanted to have a vector of complex numbers in my program, so I

  • 0

I wanted to have a vector of complex numbers in my program, so I wrote this:

[|pt 0. 0.; pt -4. 1.; pt -7. -2.; pt 4. 5.; pt 1. 1.|]

Here pt is a function of type float -> float -> Complex.t. But ocaml refused to compile this saying:

Characters 12-14:
  [|pt 0. 0.; pt -4. 1.; pt -7. -2.; pt 4. 5.; pt 1. 1.|];;
              ^^
Error: This expression has type float -> float -> Complex.t
       but an expression was expected of type int

What I wanted to do here is (obviously) include the complex number whose real part is -4 and whose imaginary part is 1. But ocaml treated what I intended to be an unary minus as a function of type int -> int ->int.

What should I write to do what I wanted to?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T11:09:49+00:00Added an answer on May 28, 2026 at 11:09 am

    Here’s how I think it goes (after reading docs and experimenting). There really are four completely different operators:

    -    Integer subtraction        int -> int -> int
    -.   Floating subtraction       float -> float -> float
    ~-   Integer unary negation     int -> int
    ~-.  Floating unary negation    float -> float
    

    If everybody used these operators, things would be clear, but unfortunately it’s also a pretty clumsy notation. In my experience the ~- and ~-. operators are rarely used. The OCaml grammar is specified to let you use the subtraction operators as unary negation operators, as in many other languages. If you do that, you often have to use extra parentheses. If you’re willing to use the specific unary operators, you don’t need the parentheses.

    I.e., you can write (as in pad’s edited answer):

    [|pt 0. 0.; pt ~-.4. 1.; pt ~-.7. ~-.2.; pt 4. 5.; pt 1. 1.|]
    

    Or you can write:

    [|pt 0. 0.; pt (-.4.) 1.; pt (-.7.) (-.2.); pt 4. 5.; pt 1. 1.|]
    

    There’s also one extra confusing factor, which is that the OCaml lexer is specified to let you use the integer subtraction operator for unary negation when you use it with a floating constant. Again, this makes the notation more like other languages. Since it’s fundamentally a binary operator, you need the parentheses here too.

    This means you can write:

    [|pt 0. 0.; pt (-4.) 1.; pt (-7.) (-2.); pt 4. 5.; pt 1. 1.|]
    

    This notation only works for negative floating constants. The other two notations work for any expression you might want to negate.

    # (-) ;;
    - : int -> int -> int = <fun>
    # (-.) ;;
    - : float -> float -> float = <fun>
    # (~-) ;;
    - : int -> int = <fun>
    # (~-.) ;;
    - : float -> float = <fun>
    
    # let f x = x +. 2.0;;
    val f : float -> float = <fun>
    
    # f ~-.5.;;
    - : float = -3.
    
    # f -.5.;;
    Characters 0-1:
      f -.5.;;
      ^
    Error: This expression has type float -> float
           but an expression was expected of type float
    # f (-.5.);;
    - : float = -3.
    
    # f -5.;;
      ^
    Error: This expression has type float -> float
           but an expression was expected of type int
    # f (-5.);;
    - : float = -3.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have wanted to try GAE since launch, but coming from ASP .NET and
I am programming in Ruby and have wanted to learn about matrices but I
I currently have a vector using my template as its type: vector<hashData> myTable; hashData
So I have declared a vector in my class header like this: ... private:
I have this situation where an EXE program imports a DLL for a single
hi when I wanted to have something like vector of vectors (elements shall be
I have a vector of three numbers as a name for a model. i.e.
Say I wanted to have a project, and one-to-many with to-do items, and wanted
If I wanted to have Python distributed across multiple processors on multiple computers, what
Say I wanted to have one variable in a class always be in some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.